1

I have a file download code in my site which worked perfectly fine untill i changed domain. I have checked all the links and changed everything and they work FINE Yet for some reason the file is downloaded corrupted and i can't figure out why.

Code:

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

$FileName has name + extension. $file_url is Working Fine. if i echo the url and try to browse to it i see the file.

Any help someone?

EDIT: I have noticed that the browser says : Resource interpreted as Document but transferred with MIME type image/png

eric.itzhak
  • 15,752
  • 26
  • 89
  • 142
  • Do you call your PHP file with parameters and stuff? – dan-lee Mar 30 '12 at 14:27
  • Ya i use GET, but this code worked fine for about a week... and i tried placing the headers on top... – eric.itzhak Mar 30 '12 at 14:29
  • Look inside the file (hex editor, or whatever): Is there is for example a php error/warning? – KingCrunch Mar 30 '12 at 14:32
  • What is the actual mime-type of the file being downloaded? It is usual to specify this as the Content-Type, rather than octet-stream, since the Content-Disposition header is what controls the download. – MrWhite Mar 30 '12 at 14:32
  • @KingCrunch It's not to a specific file i use this code to download many files and it happens to all of them. – eric.itzhak Mar 30 '12 at 14:34
  • @w3d Those files are images, but can be png,gif,jpeg etc. – eric.itzhak Mar 30 '12 at 14:35
  • @eric.itzhak OK, than look into all of them... – KingCrunch Mar 30 '12 at 14:35
  • Just to emphasize my comment above, if you are sending images then the Content-Type should be the appropriate mime-type for the image, not "application/octet-stream". Have you tried this with different browsers? – MrWhite Mar 30 '12 at 14:36
  • @KingCrunch i did, saw none. However maybe it is relavent - the files original name is in Hebrew, (though outputted differently) – eric.itzhak Mar 30 '12 at 14:38
  • Maybe your PHP file has a BOM at the start, or a space, or a PHP-notice. Download the file and check with a text/hex-editor. – ontrack Mar 30 '12 at 15:25

1 Answers1

2

If the files being downloaded are images (as mentioned in the comments), then the Content-Type header should be the appropriate mime-type for that image eg. "image/jpeg", "image/png", etc.

$size = getimagesize($file_url);
header("Content-type: {$size['mime']}");

"application/octet-stream" should not be used in this instance (often used in the past to attempt to force a download, since the browser won't necessarily know what to do with it). The "Content-Disposition: attachment" header is what triggers the download.

Also, you mention the original filename is in Hebrew... The "filename=" header should specify a filename in US-ASCII for compatibility. UTF-8 is possible but only with some additional 'messing around', and still might not be compatible with some browsers.

EDIT#1

If you are using output buffering then you might need to ensure the output buffer is empty and flushed before attempting to download the file, otherwise whatever is in the output buffer will become part of your downloaded file and it will be corrupted.

header('Content-Length: ' . filesize($file_url));
ob_clean();    // <<<<
flush();       // <<<<
readfile($file_url);

EDIT#2 - Content-Type

Following your link to the answer of that other question that suggests using application/octet-stream instead of image/jpg... the answer in that question quotes RFC 2616, however, this has been superseded by RFC 6266 where it states:

According to RFC 2616, the disposition type "attachment" only applies to content of type "application/octet-stream". This restriction has been removed, because recipients in practice do not check the content type, and it also discourages properly declaring the media type.

So, it is more correct to specify the correct mime-type for the image (as mentioned above). Although, at the end of the day, what is important is what works for you as servers/browsers do vary and don't necessarily follow the standards unfortunately.

To further back up the argument for specifying the correct mime-type, and not using "application/octet-stream", see the following accepted answer to a question on webmasters...

What could keep Chrome from downloading files?

Community
  • 1
  • 1
MrWhite
  • 43,179
  • 8
  • 60
  • 84
  • Do you get _something_ downloaded? How does this differ from the filesize() in the header? Examine the response headers sent to the browser using the browser developer tools... do they match, any extras added by the server? Are you using output buffering for anything? – MrWhite Mar 30 '12 at 15:04
  • i am getting the file downloaded, it's just won't open. – eric.itzhak Mar 30 '12 at 15:13
  • i have now realised that the browser throws error ( dunno how i didn't notice it sooner sorry ) that : Resource interpreted as Document but transferred with MIME type image/png: – eric.itzhak Mar 30 '12 at 15:22
  • What browser are you using? I think that's just telling you it's corrupt, but why... are you using the browser to display the image? (You wouldn't have gotten the error with "application/octet-stream" - but that wouldn't have fixed anything.) – MrWhite Mar 30 '12 at 15:30
  • actully i tried going back to octet-strean because of this post : http://stackoverflow.com/questions/5648967/force-download-image and it still happened. I am using Chrome and i'm not displaying the image in the browser. when i press the download button i see that warning – eric.itzhak Mar 30 '12 at 15:31
  • Want me to publish the headers? i'm new to headers stuff don't really know what it should be like... – eric.itzhak Mar 30 '12 at 15:40
  • Thank you! bloody hell the last edit solved it i think. not exactly sure because i tried alot of stuff including editing .htaccess ( not sure it did any good) But thank you kind sir! – eric.itzhak Mar 30 '12 at 15:51
  • You're welcome. I was just looking stuff up following your link to the other question. ... I'll update my answer as there is a bit much for a comment. – MrWhite Mar 30 '12 at 16:01