
10-22-05, 06:19 PM
|
|
Newbie Coder
|
|
Join Date: Jul 2004
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
|
Downloading Binary from mySQL
I have stored some binary data in a mySQL database and I am trying to create a page that will go through the database and create a page that has a series of links to download the original files. Right now the code below goes through the database and creates the link but when you click on the link rather than have the "Save file" dialog box appear is displays a page with a whole mess of data. How do I make it so that when the link is clicked the user can download it?
Quote:
<?
if(isset($_GET['id']))
{
include('configdb.php');
include('opendb.php');
$id = $_GET['id'];
$query = "SELECT name, type, size, content FROM upload WHERE id = '$id'";
$result = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $content) = mysql_fetch_array($result);
header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
echo $content;
include('closedb.php');
exit;
}
?>
<html>
<head>
<title>Download File From MySQL</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?
include('configdb.php');
include('opendb.php');
$query = "SELECT id, name FROM upload";
$result = mysql_query($query) or die('Error, query failed');
if(mysql_num_rows($result) == 0)
{
echo "Database is empty <br>";
}
else
{
while(list($id, $name) = mysql_fetch_array($result))
{
?>
<a href="download.php?id=<?=$id;?>"><?=$name;?></a> <br>
<?
}
}
include('closedb.php');
?>
</body>
</html>
|
|