Force Download issue in Safari
Posted: Wed Mar 12, 2008 11:03 am
I have a media application that I built in php. The client wanted a button for downloading the mp3 without having the visitor right click, etc. There is already handling for playing the audio online. This is strictly a download link.
I build the function to do this, addressing security issues by passing id variables to the force_download.php page and then that page pulls the files from the database based on the id.
The problem is that it words in every browser but Safari. In safari it will act like it is downloading and even places the file with the correct name on the desktop. But the file is 0kb and downloads in a second.
I have exhausted my search efforts and can't find a solution. I have tried different versions of my script below and none work. What am I missing for safari to be able to actually download the mp3? Any help will be appreciated.
$file_name is equal to the full url of the download mp3.
I build the function to do this, addressing security issues by passing id variables to the force_download.php page and then that page pulls the files from the database based on the id.
The problem is that it words in every browser but Safari. In safari it will act like it is downloading and even places the file with the correct name on the desktop. But the file is 0kb and downloads in a second.
I have exhausted my search efforts and can't find a solution. I have tried different versions of my script below and none work. What am I missing for safari to be able to actually download the mp3? Any help will be appreciated.
Code: Select all
header("Pragma: public"); // required
header("Expires: 0"); // no cache
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/octet-stream;");
header("Content-Disposition: attachment; filename=\"".basename($file_name)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ". filesize($file_name)); // provide file size
set_time_limit(0);
readfile($file_name) or die("File not found."); // push it out
exit();