php - header() download corrupted file -


Why this code does not work, the URL is a sound file:

  header ( "Location: http://biencaleta.com/musica/Billboard-70s/Grupos/Billboard%201970.ca/Simon Y Garfunkel - Bridge Over Troubled. MSA");  

Browser (Chrome) Play it instead of downloading it, but this http://23.250.9.10/~caletaco/Alkilados-mona Lisa. Works with MSA I also tried with it:

  $ file = 'http: //biencaleta.com/musica/Billboard-70s/Grupos/Billboard%201970 .ca / Simon Y Garfunkel - Bridge at Trubled.msa '; $ Basename = basename ($ file); Header ('content-description: file transfer'); Header ('content-type: application / octet-stream'); Header ('content-dispute: attachment; filename =' '.basename.' '' '); Header ('content-transfer-encoding: binary'); Set_time_limit (0); Readfile ($ file);  

But it downloads a corrupted file. Thank you

You are requesting an invalid URL, leaving the free space in place. Your browser takes care of it for you, but you have to do the work of PHP in PHP.

Starting with the filename:

  $ url = "http: //biencaleta.com/musica/Billboard-70s/Grupos/Billboard 1970.ca/Simon Y Garfunkel - Bridge over Troubled. MMS ";  

... we will use the explosion to break that URL in different ways:

  $ url = explosion ( '/', $ Url);  

We now have an array:

  var_dump ($ url); / * Array (size = 8) 0 = & gt; String 'http:' (length = 5) 1 = & gt; String '' (length = 0) 2 = & gt; String 'biencaleta.com' (length = 14) 3 = & gt; String 'music' (length = 6) 4 = & gt; String 'Billboard-70s' (length = 13) 5 = & gt; String 'Groupus' (length = 6) = 6 = & gt; String 'Billboard 1970.ca' (length = 17) 7 = & gt; String 'Simon Y Garfunkel - Bridge Over Troubled. MSA '(length = 43) * /  

Within that array, you have several parts whose URLs are encoded. There is a function for PHP, whose name is rawurlencode , you can not just throw the entire URL on the function, because it encodes the http: // part, which we Do not want to.

We save the protocol and get rid of that empty object (this comes from the "//" part of the URL):

  $ protocol = array_shift ($ url); // Save this array_shift ($ url); // it out  

Now, you can securely convert the rest to the sign:

  foreach ($ url as & amp; $ part ) $ Part = rawurlencode ($ part);  

... and return it together

  $ url = $ protocol '//'. Implode ('/', $ url);  

Compare now-encoded URLs against the original:

  enc: http://biencaleta.com/musica/Billboard-70s/Grupos/ Billboard% 201970.ca / Simon% 20Y% 20 Garfunkel% 20-% 20 Bridge% 20Over% 20 Posted on .msa orig: http://biencaleta.com/musica/Billboard-70s/Grupos/Billboard 1970.ca / Simon Y Garfunkel - The bridge over Trubled.msa  

... looks good. Now we can grab the content and download the download:

  $ content = file_get_contents ($ url); Header ('content-description: file transfer'); Header ('content-type: application / octet-stream'); Header ('content-dispute: attachment; filename =' '. $ CleanName.' '' ''); Header ('expiration: 0'); Header ('cache-control: required-modified'); Header ('pragma: public'); Die ($ content);  

You can see the $ cleanName variable ... what's that? Before the string looks encoded and messy, you can blast it basename - right before . By putting it together:

  $ url = "http://biencaleta.com/musica/Billboard-70s/Grupos/Billboard 1970.ca/Simon Y Garfunkel - Bridge Over Troubled.mms" ; $ CleanName = basename ($ url); $ Url = Explosion ('/', $ url); $ Protocol = array_shift ($ url); Array_shift ($ url); Forex Currency ($ url & amp; $ portion) $ part = rawurlencode ($ part); $ Url = $ Protocol '//'. Implode ('/', $ url); $ Contents = file_get_contents ($ url); Header ('content-description: file transfer'); Header ('content-type: application / octet-stream'); Header ('content-dispute: attachment; filename =' '. $ CleanName.' '' ''); Header ('expiration: 0'); Header ('cache-control: required-modified'); Header ('pragma: public'); Die ($ content);  

document

  • explosion -
  • rawurlencode < / Code> -
  • implode -
  • basename -
  • file_get_contents -

Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -