homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Force Download
PHP Force Download of mp3s

 7:14 am on Apr 25, 2006 (gmt 0)

Strange thing, the following script works (kind of), but it uses 'download.php.mp3' as the file name to download instead of the true file name.

I found the script and like it, but don't know why it isn't picking up the correct file name.

Here it is -

$filename = $_GET['file'];

// required for IE, otherwise Content-disposition is ignored
ini_set('zlib.output_compression', 'Off');

$emailaddress = "webmaster@garycarpenter.org";
$directory = "http://garycarpenter.org/MP3/";
$path = "$directory$filename";
putenv('TZ=PST8PDT'); // western time
$message = "File name: " . $filename . "\n\n";
$message .= "Time of the download: " . date(" F d h:ia") . "\n\n";
$message .= "Browser: " . $_SERVER['HTTP_USER_AGENT'] . "\n\n";
$message .= "Page Requested: " . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . "\n\n";
$message .= "Referer: " . $_SERVER['HTTP_REFERER'] . "\n\n";
$message .= "IP Address: ". $_SERVER['REMOTE_ADDR'] . "\n\n";
$message .= "Hostname: " . gethostbyaddr($_SERVER['REMOTE_ADDR']) . "\n\n";

if (! file_exists( $filename ) ) {
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
Header( "Content-Type: audio/mpeg" );
Header( "Content-Length:" . filesize( $path ) );
Header( "Content-Disposition: attatchment; filename=$filename" );
readfile( $path );
} else {
mail($emailaddress, "Download error" , $message, "From: Website <>");
echo "Sorry your download was unsuccessful. An email has been sent to the Webmaster. \n\n";
echo "Please use your browsers back button";


Can someone tell me what's going wrong?

Thank you!



 7:20 am on Apr 25, 2006 (gmt 0)

Ooops I forgot - here is the link I use

<a href="download.php?file=AOG-1-gooriow.mp3">

And it doesn't work in IE 6 just starts to run QT, but then nothing happens.

Thanks for your help.


 7:24 am on Apr 25, 2006 (gmt 0)

hi jami,

IE just passes the file to QT because of the content-type audio/mpeg. this so called mime-type is configured in your operating system to be handled by quicktime, so everything seems in the right order.

if you are not confident with the way your browser and/or your os handels your download, please re-configure your browser and os to do what your intention is. you can hard knock it by uninstalling quicktime for example.



 7:59 pm on Apr 27, 2006 (gmt 0)

Hey i have this script that you can use


$file = $_GET['d'];

*ini_set('zlib.output_compression', 'Off');

$file_extension = strtolower(substr(strrchr($file,"."),1));

if( $file == "" )
*echo "<html>
**<title>File not found.</title>
**File not found.
} elseif (! file_exists( $file ) )
*echo "<html>
**<title>The file $file was not found.</title>
**The file $file was not found.
switch( $file_extension )
*case "pdf": $ctype="application/pdf"; break;
*case "zip": $ctype="application/zip"; break;
*case "doc": $ctype="application/msword"; break;
*case "xls": $ctype="application/vnd.ms-excel"; break;
*case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
*case "gif": $ctype="image/gif"; break;
*case "png": $ctype="image/png"; break;
*case "jpeg":
*case "jpg": $ctype="image/jpg"; break;
*case "wav":
*case "mp3": $ctype="application/iTunes"; break;
*default: $ctype="application/force-download";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=\"".basename($file)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file));


replace the * with tabs

to download a file type download.php?d=Your File

i think that is what you wanted

Mr. Gecko


 8:29 pm on Apr 27, 2006 (gmt 0)

Yes, that's what I meant...

I know I can change things in my browser, but thanks for mentioning it anyway (some may not know).

I am a webmaster (see forum title) and so I want the script to work for everyone, and people being what they are I cannot expect to have them change their browser settings to soot my problem code :-)

I will look over 'your version' and see where mine is messed up - Thanks so much!

And thanks for all the replies.... I really appreciate it.


 10:02 pm on Apr 27, 2006 (gmt 0)

No problem


 10:10 pm on Apr 27, 2006 (gmt 0)

[edit]Whups, sorry, I read too far along this thread and somehow lost the fact that the question was about the filename. I was saying to make the content-type application/octet-stream instead of audio/mpeg but of course that won't solve the filename problem. [/edit]


[edited by: JollyK at 10:12 pm (utc) on April 27, 2006]


 10:11 pm on Apr 27, 2006 (gmt 0)


Can anyone tell me how to change this script, posted here, so that is won't assume it is in the same dir as the media files its referencing?

I would like a path feature, and tried to add it myself, but the script just kept saying it couldn't find the file - arrrggg!

Thanks in advance....


 10:13 pm on Apr 27, 2006 (gmt 0)

Jami: Your script or MrGecko's?



 11:36 pm on Apr 27, 2006 (gmt 0)

Ooops off-line for a bit... but back now.

To answer JollyK - I can follow the edit of either. They are both the same script basically, with just a couple of changes. So use either one.

I tried assigning a $directory and then having
$path = $directory$file; and I used $path everywhere $file was, but of course my thinking was to simplistic :-(


 11:44 pm on Apr 27, 2006 (gmt 0)

Jami, in your script, the following line is incorrect:
Header( "Content-Disposition: attatchment; filename=$filename" );
The filename should be in quotes
Header( "Content-Disposition: attatchment; filename=\"$filename\"" );

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved