问题
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
回答1:
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?
来源:https://stackoverflow.com/questions/9944869/forced-file-download-iscorrupted