Wolverine68 Posted June 2, 2008 Share Posted June 2, 2008 I posted a thread previously requesting a php script that will force a download of mp3 files. I did get one but can't get it to work. Here's the script: <?php $filename = $_GET["http://www.mywebsite.com/audio/audiofile.mp3"]; // required for IE, otherwise Content-disposition is ignored if(ini_get('zlib.output_compression')) ini_set('zlib.output_compression', 'Off'); // addition by Jorg Weske $file_extension = strtolower(substr(strrchr($filename,"."),1)); if( $filename == "" ) { echo "<html><title>eLouai's Download Script</title><body>ERROR: download file NOT SPECIFIED. USE force-download.php?file=filepath</body></html>"; exit; } elseif ( ! file_exists( $filename ) ) { echo "<html><title>eLouai's Download Script</title><body>ERROR: File not found. USE force-download.php?file=filepath</body></html>"; exit; }; switch( $file_extension ) { case "pdf": $ctype="application/pdf"; break; case "exe": $ctype="application/octet-stream"; 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; default: $ctype="application/force-download"; } 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: $ctype"); // change, added quotes to allow spaces in filenames, by Rajkumar Singh header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" ); header("Content-Transfer-Encoding: binary"); header("Content-Length: ".filesize($filename)); readfile("$filename"); exit(); ?> _____________________ I get the following error message "ERROR: File not found. USE force-download.php?file=filepath" As you can see, I defined the exact path in the first line, $filename = $_GET["http://www.mywebsite.com/audio/audiofile.mp3"]; (By the way, the path that I have posted here is not the actual path. Using a generic name for demonstration purposes) I thought I had to leave $filename as it is where it appears in other parts of the script since I already defined $filename in the first line of the script. So, then I replaced every instance of $filename with the exact path. I tried using both single and double quotes but it didn't work. Anyone have any suggestions? Link to comment https://forums.phpfreaks.com/topic/108369-solved-cant-get-a-force-download-script-to-work/ Share on other sites More sharing options...
BlueSkyIS Posted June 2, 2008 Share Posted June 2, 2008 filename should be the absolute path to the file, not a URL. for instance: $filename = '/home/users/auser/public_html/audio/audiofile.mp3'; Link to comment https://forums.phpfreaks.com/topic/108369-solved-cant-get-a-force-download-script-to-work/#findComment-555548 Share on other sites More sharing options...
haku Posted June 2, 2008 Share Posted June 2, 2008 A relative path will work as well. Link to comment https://forums.phpfreaks.com/topic/108369-solved-cant-get-a-force-download-script-to-work/#findComment-555562 Share on other sites More sharing options...
Wolverine68 Posted June 2, 2008 Author Share Posted June 2, 2008 I thought "http://www.mywebsite.com/audio/audiofile.mp3" was the absolute path...no? I changed it to $filename = $_GET['/audio/audiofile.mp3']; Still get the error. I realized the .php file, mp3 file, and the webpage that calls the .php file are all located in the audio folder, so I changed it to: $filename = $_GET['audiofile.mp3']; Still get the error. Also, do I need to replace every instance of where $filename appears in the script with the path? I defined it in the first line so I didn't think I had to. Link to comment https://forums.phpfreaks.com/topic/108369-solved-cant-get-a-force-download-script-to-work/#findComment-555586 Share on other sites More sharing options...
haku Posted June 2, 2008 Share Posted June 2, 2008 Change: $filename = $_GET['/audio/audiofile.mp3']; to $filename = "/audio/audiofile.mp3"; Note: you may need to remove the leading slash, I have a feeling it's going to cause you problems. Link to comment https://forums.phpfreaks.com/topic/108369-solved-cant-get-a-force-download-script-to-work/#findComment-555590 Share on other sites More sharing options...
Wolverine68 Posted June 2, 2008 Author Share Posted June 2, 2008 I've tried all of the following: $filename = "/audio/audiofile.mp3"; $filename = "audio/audiofile.mp3"; $filename = $_GET["/audio/audiofile.mp3"]; $filename = $_GET["audio/audiofile.mp3"]; $filename = $_GET['/audio/audiofile.mp3']; Still getting the error. $filename = $_GET['audio/audiofile.mp3']; Link to comment https://forums.phpfreaks.com/topic/108369-solved-cant-get-a-force-download-script-to-work/#findComment-555612 Share on other sites More sharing options...
BlueSkyIS Posted June 2, 2008 Share Posted June 2, 2008 you don't want to use the $_GET array as you are unless you have a form field with name='audio/audiofile.mp3' assuming the name of the file is in a form field called 'file_name', then you want $_GET['file_name']; Link to comment https://forums.phpfreaks.com/topic/108369-solved-cant-get-a-force-download-script-to-work/#findComment-555615 Share on other sites More sharing options...
haku Posted June 2, 2008 Share Posted June 2, 2008 Give us the full url to the force download script, as well as to the mp3 file. Then we can tell you what the relative path will be. Link to comment https://forums.phpfreaks.com/topic/108369-solved-cant-get-a-force-download-script-to-work/#findComment-555619 Share on other sites More sharing options...
Wolverine68 Posted June 2, 2008 Author Share Posted June 2, 2008 Can I send it to a private e-mail address? Link to comment https://forums.phpfreaks.com/topic/108369-solved-cant-get-a-force-download-script-to-work/#findComment-555635 Share on other sites More sharing options...
Wolverine68 Posted June 2, 2008 Author Share Posted June 2, 2008 Check your "My Messages" area of your account. I sent you the full url paths there. Link to comment https://forums.phpfreaks.com/topic/108369-solved-cant-get-a-force-download-script-to-work/#findComment-555688 Share on other sites More sharing options...
Wolverine68 Posted June 2, 2008 Author Share Posted June 2, 2008 I resolved it. The download.php and mp3 files were in the same directory. I just needed to reference it as $filename = 'audiofile.mp3' Good grief. What was I thinking. Thanks, haku Link to comment https://forums.phpfreaks.com/topic/108369-solved-cant-get-a-force-download-script-to-work/#findComment-556087 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.