Jump to content

Force Download Mp3


CRO8

Recommended Posts

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();

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by TCH-Bruce
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • 1 year later...

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>

Link to comment
Share on other sites

  • 10 months later...
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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 1 year later...
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!!!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...