Primary links
Force download files using PHP
admin — Wed, 07/03/2007 - 3:06pm
The following code illustrates this method. Save this as download.php and pass the file as a parameter to it.
<?
//Get the file name passed to the script
$filename = $_GET['dl'];
//Absolute path the the file to be downloaded
$dlfile = "/var/www/files/".$filename;
// open the file and read it to a string
$file_from = fopen($dlfile,"r");
$i = 0;
$data = "";
while ( $row = fread($file_from,filesize($dlfile)) ) {
++$i;
$data .= $row;
}
fclose($file_from);
/*
By using the header option we tell the browser that the file format is not
something which it can open. In the second line we are specifying the default file name.
*/
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename="".$filename."";");
header("Content-Transfer-Encoding: binary");
header("Cache-Control: cache, must-revalidate");
header("Pragma: public");
header("Expires: 0");
/* We are storing the data and sending it to the client machine through the file download.*/
echo $data;
exit();
?>
User login
Follow Us
Who's online
Who's new
- Nisha
- linnaeus
- Yameen
- TalleyReedy
- admin

