CRO8 Posted September 3, 2004 Posted September 3, 2004 Im using the php script Don suggested from hotscripts. There is no readme file so I am not quite sure how to incorporate this with my site. Basically I dont want to give folks an option to play the mp3s from my site and force them to download them instead. Where do I enter the names of the mp3s Im using? Thanks! ><?php $filename = $_GET['file']; $file-extension = substr( $filename,-3 ); 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 "jpg": $ctype="image/jpg"; 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: public"); header("Content-Type: $ctype"); $user_agent = strtolower ($_SERVER["HTTP_USER_AGENT"]); if ((is_integer (strpos($user_agent, "msie"))) && (is_integer (strpos($user_agent, "win")))) { header( "Content-Disposition: filename=".basename($filename).";" ); } else { header( "Content-Disposition: attachment; filename=".basename($filename).";" ); } header("Content-Transfer-Encoding: binary"); header("Content-Length: ".filesize($filename)); readfile("$filename"); exit(); ?> Quote
purplespider Posted September 3, 2004 Posted September 3, 2004 Looks like you need to copy that code into a file and call it something like mp3download.php Then to link to an mp3 to download you'd use something like "www.yoursite.com/mp3download.php?filename=mymp3.mp3" if not, try "www.yoursite.com/mp3download.php?file=mymp3.mp3". Let me know if that works, if not, I'll try the script out for ya and see what I can work out. James Quote
borfast Posted September 3, 2004 Posted September 3, 2004 It should work with the second option James suggested ("www.yoursite.com/mp3download.php?file=mymp3.mp3") Quote
CRO8 Posted September 3, 2004 Author Posted September 3, 2004 oh gotcha gotcha. yup ok let me try. thanks! nope . . . not working error on line 5: >$file-extension = substr( $filename,-3 ); if( $filename == "" ) { Quote
TCH-Bruce Posted September 3, 2004 Posted September 3, 2004 (edited) You are going to need to modify this section. 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 "jpg": $ctype="image/jpg"; break; default: $ctype="application/force-download"; } Add the following above the line that starts with "default": case "mp3": $ctype="application/mp3"; break; Edited September 3, 2004 by TCH-Bruce Quote
CRO8 Posted September 3, 2004 Author Posted September 3, 2004 ok thanks Bruce- let me do it later tonight. Nice man- thanks. See if it works. Thanks. Quote
TCH-Bruce Posted September 3, 2004 Posted September 3, 2004 CRO8, I don't think that you need to add what I said above. That is an actual error in the script that it is reporting. I am trying to figure out why but it eludes me at the moment. If I get time I will look at it today. Anyone else have any ideas, please reply. Quote
kaseytraeger Posted September 3, 2004 Posted September 3, 2004 There was a similar discussion about "forcing a download box" going on here a while back. It might apply to your situation because you want to force the MP3s to be downloaded instead of played. http://www.totalchoicehosting.com/forums/i...=force+download Quote
TCH-Bruce Posted September 3, 2004 Posted September 3, 2004 Ok, I couldn't get that script to work and I decided why not rewrite it and make it simple. So here is what I have written and it does work. <?php/* setup - create a PHP file called fdownload.php with the lines below. Change the $filedir to point to you path on the server Call from a link like this: */ $filedir = "/home/yourcpanelname/public_html/" ; $file = "$filedir".$_GET['file'].""; if (file_exists(basename($file))) { header("Content-Description: File Transfer"); header("Content-Type: application/force-download"); header("Content-Disposition: attachment; filename=".basename($file)); readfile($file); } else { echo "$file "; echo basename($file); echo " No File Found"; } ?> Copy and paste the above script into a blank file and name it fdownload.php. You only need to place the path to your files in the $filedir variable with a trailing "/". Call the script using a link as follows: your link Quote
CRO8 Posted September 3, 2004 Author Posted September 3, 2004 it works! Thanks Don, thanks all! Quote
TCH-Bruce Posted September 3, 2004 Posted September 3, 2004 Your welcome. Glad it worked for you. Quote
cmpowell Posted December 9, 2005 Posted December 9, 2005 Cool Script. But, in Internet Explorer it names the file "fdownload.php". Any suggestions? Ok, I couldn't get that script to work and I decided why not rewrite it and make it simple. So here is what I have written and it does work.Copy and paste the above script into a blank file and name it fdownload.php. You only need to place the path to your files in the $filedir variable with a trailing "/". Call the script using a link as follows: <a href="http://www.******/fdownload.php?file=filename">your link</a> Quote
TCH-Bruce Posted December 9, 2005 Posted December 9, 2005 Welcome to the forums cmpowell Do you have a link we can try? The script works fine for me in IE. Quote
cj_follett Posted November 3, 2006 Posted November 3, 2006 Ok, I couldn't get that script to work and I decided why not rewrite it and make it simple. So here is what I have written and it does work.Copy and paste the above script into a blank file and name it fdownload.php. You only need to place the path to your files in the $filedir variable with a trailing "/". Call the script using a link as follows: <a href="http://www.******/fdownload.php?file=filename">your link</a> Okay, I tried your re-written example and it worked -- sort of. I changed the $filedir path to / since everything was in the same directory. The download worked, but the file was not correct. Then I changed the path to ./ and it worked fine. I seem to recall something from many years ago why you need to use ./, but the explanation escapes me. Quote
TCH-Bruce Posted November 3, 2006 Posted November 3, 2006 Welcome to the forums cj_follett Glad you got it working. If the files are in a subfolder ./ will be the directory you are executing from / would be your public_html folder. Quote
cj_follett Posted November 4, 2006 Posted November 4, 2006 Welcome to the forums cj_follett Glad you got it working. If the files are in a subfolder ./ will be the directory you are executing from / would be your public_html folder. Thanks for the welcome. Your script helped me solve a problem I was having with Firefox. It seems that Firefox doesn't like a simple <A HREF ...> to download an executable. Anyway, both the php file and the file to download are located in the same directory for the time being to run some tests. If / is pointing to root (in my case htdocs, not public_html), then was it just downloading the first file it encountered? Shouldn't it give me an error that the file I was looking for was not found? Quote
TCH-Bruce Posted November 4, 2006 Posted November 4, 2006 Yes, it should give an error unless the filename is not being parsed properly. Are you using this for mp3 files or are some other type of file extension. Quote
TCH-Bruce Posted November 4, 2006 Posted November 4, 2006 The reason I asked what type of files you were using is because I use this script on my site. It allows for downloads or opening of files. Quote
carbonize Posted November 4, 2006 Posted November 4, 2006 In theory you could automate a lot of this so long as you are not streaming any MP3's. I've not tested but in theory you could use .htaccess to redirect all requests for files ending in .mp3 to a download script and then parse the mp3 name from the url ( $_SERVER['REQUEST_URI'] or getenv('REQUEST_URI'))and start the download IF the file exists. Quote
mixmason Posted July 24, 2008 Posted July 24, 2008 Ok, I couldn't get that script to work and I decided why not rewrite it and make it simple. So here is what I have written and it does work. Copy and paste the above script into a blank file and name it fdownload.php. You only need to place the path to your files in the $filedir variable with a trailing "/". Call the script using a link as follows: <a href="http://www.******/fdownload.php?file=filename">your link[/url] Hey Bruce, I know this is an extremely old forum post but I'm trying to enable this to my site and its not working. I'm highly experienced with HTML but sadly don't have much PHP experience. Having difficulty linking this code into a page to actually make it work. Do I need to change my html page into a php file? If so, how is this done? ANY HELP would be awesome. Thanks!!! Quote
TCH-Bruce Posted July 25, 2008 Posted July 25, 2008 Only the fdownload.php file needs to be php. I haven't used this in a very long time, not sure if it works any longer. Quote
mixmason Posted July 25, 2008 Posted July 25, 2008 Only the fdownload.php file needs to be php. I haven't used this in a very long time, not sure if it works any longer. Thanks for that, Bruce. Have a good day! Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.