Welcome to WebmasterWorld Guest from 54.159.12.17

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Force Download

PHP Force Download of mp3s

     

Jami

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

5+ Year Member



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 -

[PHP]
<?php
$filename = $_GET['file'];

// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression'))
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";
}
exit();

?>
[PHP]

Can someone tell me what's going wrong?

Thank you!

Jami

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

5+ Year Member



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.

hakre

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

WebmasterWorld Senior Member 10+ Year Member



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.

--hakre

MrGecko

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

5+ Year Member



Hey i have this script that you can use

<?php

$file = $_GET['d'];

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

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

if( $file == "" )
{
*echo "<html>
***<head>
**<title>File not found.</title>
**</head>
**<body>
**File not found.
**</body>
**</html>";
*exit;
} elseif (! file_exists( $file ) )
{
*echo "<html>
***<head>
**<title>The file $file was not found.</title>
**</head>
**<body>
**The file $file was not found.
**</body>
**</html>";*exit;
};
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));
readfile("$file");
exit();

?>

replace the * with tabs

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

i think that is what you wanted

Mr. Gecko

Jami

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

5+ Year Member



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.

MrGecko

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

5+ Year Member



No problem

JollyK

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

10+ Year Member



[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]

JK

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

Jami

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

5+ Year Member



QUESTION:

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....

JollyK

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

10+ Year Member



Jami: Your script or MrGecko's?

JK

Jami

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

5+ Year Member



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 :-(

jezra

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

10+ Year Member



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\"" );
 

Featured Threads

Hot Threads This Week

Hot Threads This Month