The list of all PHP and Mysql posts

Showing posts with label How to make a download link. Show all posts
Showing posts with label How to make a download link. Show all posts

How to make a download link

To make a download link , just put a hyper link to a an other page contains download headers.
I explain it by an example:

Put a hyper link to download page:

Code:

<a href="music2_download.php">download</a></font></b></p>

If user clicks on this link , he is redirected to  "music2_download.php" page. Put the following codes in "music2_download.php" file.

code:

<?php
include "auth.inc"; // This code insure that just registered users can download our file
?>
<?php
$FileName = "/music2.swf";  // the name of the file that is downloaded
$FilePath = "my_music";  // the folder of the file that is downloaded , you can put the file in a folder on the server just for more order

$size = filesize($FilePath . $FileName) ;
header("Content-Type: application/force-download; name=\"". $FileName ."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ". $size ."");
header("Content-Disposition: attachment; filename=\"". $FileName ."\"");
header("Expires: 0");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
echo (readfile($FilePath . $FileName));
?>



------------------------------------------------------------------------------------------

The following is an extra part for this post :

You may want to use just one "download.php" page for all of your downloads. You can send the file name through a hidden variable , The form below allows to send the file name to "download.php" page :


code:



<FORM ACTION="download.php" METHOD=POST>


<INPUT TYPE="hidden" NAME="music_name"    VALUE= "music1.swf"  >


<BR>
To download music1: 
<INPUT TYPE="SUBMIT" NAME="download_music1"  VALUE=" download_music1 ">


</FORM>


<FORM ACTION="download.php" METHOD=POST>


<INPUT TYPE="hidden" NAME="music_name"    VALUE= "music2.swf"  >


<BR>
To download music2: 
<INPUT TYPE="SUBMIT" NAME="download_music2"  VALUE=" download_music2">


</FORM>




Now Just add this line to "download.php"  , to give the file name to "download.php:


$FileName = "/".$_POST["music_name"]// put the file name in its variable.


Now you have a Universal "download.php".