PHP Force Download :: Fixing Safari Download Header Issues
October 26, 2007
Problem:
We’d got this client requesting a download script written in PHP. The Script should be able to run in Mozilla Firefox, MS IE, and Safari. Sounds simple enough right? I mean this could simply be done by just sending the following headers:
—
header(“Expires: 0″);
header(“Cache-Control: must-revalidate, post-check=0, pre-check=0″);
header(“Cache-Control: private”,false);
header(“Content-Transfer-Encoding: binary”);
header(“Content-Type: video/x-ms-wmv”);
header(“Content-Length: “.@filesize($filepath));
header(“Content-Disposition: attachment; filename=\”".$filename.“\”;”);
—
We’ve been using this header to force browser to download contents since day one. We thought nothing was wrong with it. That’s before we tested it with Safari. For some reason Safari just displays a blank white page. No error messages no nothing.
Solution:
Apparently this is caused by Safari not being able to identify what file it is accessing. It doesn’t know how to deal with “video/x-ms-wmv” with is the mime-type for *.wmv files. The mime-type it recognizes as downloadable would be “application/octet-stream”. So by changing the header being passed you would be able to force Safari to download the contents. Of course you’ll still need to attach the contents after sending the headers. The resulting headers will then be:
—
header(‘Expires: 0′);
header(‘Last-Modified: ‘ . gmdate (‘D, d M Y H:i:s’, filemtime ($filepath)) . ‘ GMT’);
header(‘Pragma: public’);
header(‘Cache-Control: must-revalidate, post-check=0, pre-check=0′);
header(‘Accept-Ranges: bytes’);
header(‘Content-Length: ‘ . @filesize($filepath));
header(‘Content-Type: Application/octet-stream’);
header(‘Content-Disposition: attachment; filename=”‘ . $filename. ‘”‘);
header(‘Connection: close’);
—
Source:
Dexterity Software Forums – Hiding download path in PHP
Note:
I don’t know why Safari developers haven’t addressed this issue yet as not all web developers comply with their obsolete method. There are numerous post in the web stating that Safari users are unable to download anything from the web. This is probably cause by URL masking scripts as developers would like to hide the path to their files. Content-type should be indicative and specific to its content. But for the meantime these headers will serve its purpose until Safari and other browsers recognize more mime-types.
Filed under: PHP, Safari |
Comments (2)
Related Links:

I Tried this out on Windows Safari and it doesn’t work. It always shows the file as 0 bytes.
Hi Tony,
Could you send me a sample of your PHP script. My guess it that the script you’re using is not able to output the file’s content.
You could send it at ronald@maravillaclan.net.