View Full Version : Save as popup window has disappeared

11-27-2010, 10:05 AM
Hello all.

I've made a little button to allow users to download mp3 files. It's just a hyperlink to the following php file, which I found on the internet to do this job:


$mp3 = $_GET['mp3'];
$file = 'recordings/'.$mp3;

if(ini_get('zlib.output_compression')) {
ini_set('zlib.output_compression', 'Off');

if ( file_exists($file) ) {
header("Pragma: public");
header('Expires: '.gmdate('D, d M Y H:i:s').' GMT');
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/mp3");
header('Content-Disposition: attachment; filename="'.$mp3.'"');
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".@filesize($file));
@readfile($file) OR die("<html><body OnLoad=\"javascript: alert('Unable to read file!');history.back();\" bgcolor=\"#F0F0F0\"></body></html>");
} else {
die("<html><body OnLoad=\"javascript: alert('File not found!');history.back();\" bgcolor=\"#F0F0F0\"></body></html>");


On my localhost it works fine; I click the button and the "Save as" dialogue window pops up. However, on my webserver it doesn't; instead it opens the mp3 file.

Anybody got any suggestions?



11-27-2010, 03:42 PM
The only thing I can think of that would explain what you have described is that the headers are sent prior to the headers for the attachment.
Enable your error reporting:

ini_set('display_errors', 1);

And run it on the host. Does it whine about the headers already sent? You may want to check that $_GET as well with:

$mp3 = isset($_GET['mp3']) ? $_GET['mp3'] : '';

Or just a suppressor to stop it from throwing possible errors as well.