Click to See Complete Forum and Search --> : PHP file download script
sabatier
08-14-2007, 05:38 AM
Hi,
I want create a download dialog on my web page to download files on a server. Could someone provide me with simple PHP script to put on my web page to do this? i.e something that goes into
<a href="php script">Download</a>
Any help appreciated,
Ruth
TJ111
08-14-2007, 07:42 AM
Here's the script I use to download files from a database:
<?php
session_start();
$userAccess = $_SESSION['level'];
if(isset($_GET['id']))
{
//download file
include ('dbconnect.php');
$id = $_GET['id'];
$query = "SELECT name, type, size, content FROM upload WHERE id='$id' AND access<='$userAccess'";
$result = mysql_query($query) or die(mysql_error());
list($name, $type, $size, $content) = mysql_fetch_array($result);
header('Cache-Control: maxage=3600');
header('Pragma: public');
header("Content-length: $size");
header("Content-type: $type");
header('Content-Disposition: attachment; filename="'.$name.'"');
echo $content;
mysql_close($db);
exit;
}
?>
And the link to download the file looks like this:
<a href="download.php?id=37">Download</a>