I have a lot of MP3 files that I want to make available to users. so far I have about 70 MP3's. I'm trying to use PHP to force download these mp3's from a link! is this possible?
<?php
function test($file,$path){
//force download an mp3 file
}
?>
<html><head><title>test</title></head><body>
<a href="<?php test(Rom1_1.mp3, Romans/Rom1_1.mp3); ?>">click here</a>
</body></html>is this possible? I know you can do this if I put the force download code on a seperate download.php file! but I have over 70 mp3's. is there an easier way? thank You!
No, PHP processes before the HTML is outputted to the page. It doesn't stop and wait for the user to click on the link then continue running the code....
What the user sees is just the finished product, after the factory has finished making it. The Factory never sends the product out half finished and then waits for the person to send back a reply with the product, so they can then finish building it.
What you need is something like a this:
INDEX:
<html>
<head>
<title>test</title>
</head>
<body>
<a href="DOWNLOAD_MP3.PHP?mp3=Rom1_1.mp3">Download Romans 1:1</a>
</body>
</html>DOWNLOAD_MP3:
<?php
//If the mp3 posted variable is blank or too big (hacking attempt)
//send them to the home page...
if ( (!isset($_GET["mp3"])) || (strlen($_GET["mp3"]) > 100) ) {
header("Location: http://www.example.com/index.php");
} else {
//if there is a posted Mp3 set then do this...
$mp3Name = trim(addslashes(strip_tags($_GET["mp3"]))); //which would be "Rom1_1.mp3"
$filename = "Romans/$mp3Name";
header("Content-Length: " . filesize($filename));
header('Content-Type: audio/mpeg');
header("Content-Disposition: attachment; filename= $mp3Name");
readfile($filename);
?>Thank You once again David! works perfectly! with just one correction!
I just added this
readfile($filename);
} // you forgot to close the else statement!
?>You could also add a directory name by using the "&" character:
<html>
<head>
<title>test</title>
</head>
<body>
<a href="DOWNLOAD_MP3.PHP?mp3=Rom1_1.mp3&book=Romans">Download Romans 1:1</a>
</body>
</html>DOWNLOAD_MP3:
<?php
//If the mp3 posted variable is blank or too big (hacking attempt)
//send them to the home page...
if ( (!isset($_GET["mp3"])) || (strlen($_GET["mp3"]) > 100)
(!isset($_GET["book"])) || (strlen($_GET["book"]) > 100) ) {
header("Location: http://www.example.com/index.php");
} else {
//if there is a posted Mp3 set then do this...
//which would be "Rom1_1.mp3"
$mp3Name = trim(addslashes(strip_tags($_GET["mp3"])));
//which would be "Romans"
$dir = trim(addslashes(strip_tags($_GET["book"])));
$filename = $dir. '/' .$mp3Name;
if (file_exists($filename)) {
header("Content-Length: " . filesize($filename));
header('Content-Type: audio/mpeg');
header("Content-Disposition: attachment; filename= $mp3Name");
readfile($filename);
} else {
echo "Error: could not find File!";
}
}
?>I was thinking on something like that so I adjusted your script to the following!
HTML code
<a href="downloadMP3.php?mp3=Romans/Rom1_2-7.mp3">Download Romans 1:2-7</a><br/>
<a href="downloadMP3.php?mp3=2kings/2Kings1.mp3">Download 2Kings 1</a><br/>PHP code
<?php
if ( (!isset($_GET["mp3"])) || (strlen($_GET["mp3"]) > 100) ) {
header("Location: http://www.cccornerstone.org");
} else {
$mp3Name = basename($_GET["mp3"]);
$filename = ($_GET["mp3"]);
header("Content-Length: " . filesize($filename));
header('Content-Type: audio/mpeg');
header("Content-Disposition: attachment; filename= $mp3Name");
readfile($filename);
}
?>This also worked! Many thanks! God Bless!
Ore you could just use javascript in the href... href="javascript:download(url)" where the downloadfunction is set earlier...
Alxandr
The problem is that sometimes, JavaScript has been killed in the users browser (For safely reasons). So, by keeping JS out of the code (using a simple download link) you make sure that 100% of all your users can access the content.
Plus why go to all the extra work to make a download function in JS that would only pass the same information to the download page?
There's no security in that script. What's to prevent the user from downloading a serious file by using a path like ../ to access your database information?
Run
<?php
$filename=filename($_GET['mp3']);
?>An experienced hacker would be able to find your database information within two or three file downloads, unless you are careful about the information you recieve from $_GET or $_POST. Especially when it involves file operations.
And as a final thought, Content-Type: application/force-download