0

I have created a download.php with the following headers:

            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename='.$archivo);
            header('Content-Transfer-Encoding: binary');
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . filesize($ruta));
            readfile($ruta);

But I cannot see the file. I think it's corrupted but I don't know why. This is begining of the output:

%PDF-1.4 %âãÏÓ 3 0 obj <>stream xœ–wTSهϽ7½P’Š”ÐkhRH ½H‘.*1 JÀ"6DTpDQ‘¦2(à€£C‘±"Š…Q±ëDÔqp–Id­ß¼yïÍ›ß÷~kŸ½ÏÝgï}ÖºüƒÂLX €¡Xáçň‹g`ðlàp³³BøF™|ØŒl™ø½º ùû*Ó?ŒÁÿŸ”¹Y"1P˜ŒçòøÙ\É8=Wœ%·Oɘ¶4MÎ0JÎ"Y‚2V“sò,[|ö™e9ó2„<ËsÎâeðäÜ'ã9¾Œ‘`çø¹2¾&cƒtI†@Æoä±|N6(’Ü.æsSdl-c’(2‚-ãyàHÉ_ðÒ/XÌÏËÅÎÌZ.$§ˆ&\S†“‹áÏÏMç‹ÅÌ07#â1Ø™YárfÏüYym²";Ø8980m-m¾(Ô]ü›’÷v–^„îDøÃöW~™ °¦eµÙú‡mi]ëP»ý‡Í`/в¾u}qº|^RÄâ,g+«ÜÜ\KŸk)/èïúŸC_|ÏR¾Ýïåaxó“8’t1C^7nfz¦DÄÈÎâpù柇øþuü$¾ˆ/”ED˦L L–µ[Ȉ™B†@øŸšøÃþ¤Ù¹–‰ÚøÐ–X¥!@~(* {d+Ðï}ÆGù͋љ˜ûÏ‚þ}W¸LþÈ$ŽcGD2¸QÎìšüZ4 E@ê@èÀ¶À¸àA(ˆq`1à‚D €µ ”‚­`'¨u 4ƒ6ptcà48.Ë`ÜR0ž€)ð Ì@„…ÈR‡t CȲ…XäCP”%CBH@ë R¨ª†ê¡fè[è(tº C· Qhúz#0 ¦ÁZ°l³`O8Ž„ÁÉð28.‚·À•p|î„O×àX ?§€:¢‹0ÂFB‘x$ !«¤i@Ú¤¹ŠH‘§È[EE1PL”ʅ⢖¡V¡6£ªQP¨>ÔUÔ(j õMFk¢ÍÑÎèt,:‹.FW ›Ðè³èô8úƒ¡cŒ1ŽL&³³³ÓŽ9…ÆŒa¦±X¬:ÖëŠ År°bl1¶ {{{;Ž}ƒ#âtp¶8_\¡8áú"ãEy‹.,ÖXœ¾øøÅ%œ%GÑ.....and so on

If I open same pdf with notepad:

%PDF-1.4
%âãÏÓ
3 0 obj
<</Alternate/DeviceRGB/Length 2596/N 3/Filter/FlateDecode>>stream
xœ–wTSهϽ7½P’Š”ÐkhRH
½H‘.*1  JÀ...and so on

What's the problem? Of course same happens with zip or other formats, but I will use pdfs.

EDIT: I think the problem is that I do is_file($file) before headers. If I delete that, works. Why¿?¿

Thank you!!!

legami
  • 1,303
  • 6
  • 22
  • 31
  • 1
    It doesn't look corrupted, you're just seeing the actual PDF document format as text instead of it opening with a PDF viewer. – nickb Feb 19 '13 at 16:11
  • if you download the file directly, does it work? If so, take a look at the file and the response headers, and see how they differ from when you download it via PHP. – SDC Feb 19 '13 at 16:12
  • With absolute url works, opening as a PDF. That's what I want using download.php – legami Feb 19 '13 at 16:14
  • So try setting the Content-Type header to the correct mime type for PDF files (application/pdf) – Mark Baker Feb 19 '13 at 16:17
  • Do you mean application/pdf ? I did it with same result! Or what do you mean? Thanks – legami Feb 19 '13 at 16:18

1 Answers1

1
header('Content-disposition: attachment; filename=yourfile.pdf');
header('Content-type: application/pdf');
readfile('yourfile.pdf');

Should work...

napolux
  • 15,574
  • 9
  • 51
  • 70
  • 1
    exactly how I do it works perfectly (although I changed it a little to have dynamic content-type so I can stream any files from my platform :) ) – Dave Feb 19 '13 at 16:24
  • Well, he was asking for a PDF solution. :) – napolux Feb 19 '13 at 16:28
  • That doesn't work. I think readfile needs the complete path and application/pdf is what I tried before (as commented in my comment above). Any idea? Can be because of the server configuration? Some php.ini option... :? – legami Feb 20 '13 at 08:18
  • @legami well give it the complete file path then? if you're streaming the data from a blob you don't need the readfile you just spit out the data stream directly to stream and the dispo sets the filename the user will be prompted to isntall and the content type will tell it its a pdf. if thats not working for you are you sure you have the header elements as the very very first thing on your download.php – Dave Feb 20 '13 at 08:38
  • Of course what i did is give the complete path. But doesn't work. How can I do it without readfile? I'm sorry I don't understand what you say. Could you answer with an example or piece of code? Before the headers I have some includes, session_start, and some mysql queries. – legami Feb 20 '13 at 08:46