<!-- 123 is the file id in db -->
<a href="download.php?id=123">Download</a>
2. connection.php
<?php
// connect to database
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass)or die('Error connecting to mysql');
$dbname = 'petstore';
mysql_select_db($dbname);
?>
3. download.php
<?php
//connect to db
include('connection.php');
// get file id from db
if(isset($_GET['id']))
{
// if id is set then get the file with the id from database
$id = $_GET['id'];
$result = mysql_query("SELECT filename, content_type, size, stored_filename FROM uploads WHERE id = '$id'") or die('Error, query failed');
list($name, $type, $size, $stored) = mysql_fetch_array($result);
$attachfile = $stored;
$file = str_replace(' ', '_', $name);
$actualsize = filesize($attachfile);
$image_data = (fread(fopen("$attachfile", "r"),filesize($attachfile)));
header("Content-Type: $ ");
header("Content-Disposition: attachment; filename = $file");
header("Content-Length: $actualsize");
echo $image_data;
fclose($attachfile);
exit;
}
?>
No comments:
Post a Comment