Code2Design.com

User login

The Layout

Programming

Graphic Design

Resources

Navigation

C2D Projects

Unsystematic Affiliates

Tutorials-db Project 62 Photoshop Star Tutorial Search 

Change Language

Who's online

There are currently 0 users and 7 guests online.

<a href = php function ???

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.mp3Romans/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!


Submitted by AlfonsoM on February 28, 2007 - 8:31pm.
printer friendly version

PHP Mp3 Download

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!

Thank You once again David! works perfectly! with just one correction!

I just added this
readfile($filename);
} // you forgot to close the else statement!
?>


New Mp3

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!";
}

}
?>

file_exists


I was thinking on something

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!


Javascript

Ore you could just use javascript in the href... href="javascript:download(url)" where the downloadfunction is set earlier...

Alxandr


Javascript Problems

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?


CAREFUL!!!

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']); 
?>
to reduce the filename to the current directory. It would also be wise to strip the .mp3 part of your link and just include the filename without the extension. The reason being that they might be able to change the extension and download something else in that directory.

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


Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br> <br /> <h3>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use BBCode tags in the text, URLs will be automatically converted to links
More information about formatting options



Like what you see?

Why not add more? C2D is looking for other Christian Web Masters who would like to help write articles for this site. If you have expericance in FLASH, CSS/HTML, PHP/MySQL, PhotoShop/GIMP, Blender, Javascript, or just General Design - our users would love to hear what you have to say. Contact Us

delicious   digg   reddit   magnoliacom   newsvine   furl   google   yahoo   technorati