Mysql Tutorial – Download files from MYSQL database
Posted on | March 17, 2009 | 8 Comments
Hello Friends ,
Once we upload file in Mysql database , we need to download file from Mysql database .
When we upload a file to MYSQL database we also save the file type and length.
These were not needed for uploading the files but is needed for downloading the files from the database.
Download page shows the list of file names stored in database. And it shows the download link as below. e.g. download.php?id=1
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>Download files from MYSQL database</title>
</head>
<body><?php
$dbhostname = ‘localhost’;
$dbusername = ‘root’;
$dbpassword = ”;
$connection = mysql_connect($dbhostname, $dbusername, $dbpassword)
mysql_select_db(“testing”);$sql = “SELECT id, name FROM tbl_upload”;
$res = mysql_query($sql);
if(mysql_num_rows($res) == 0)
{
echo “There is no uploaded files in database. <br />”;
}
else
{
while(list($id, $name) = mysql_fetch_array($res))
{
?>
<a href=”download.php?id=<?php echo $id;?>”><?php echo $name;?></a>
<?php
}
}
mysql_close($connection);?>
</body>
</html>
When you click the download link, we can identify the file to get from mysql database using the id passed by GET method ($_GET['id']) . Below is the code for downloading files from MySQL Database.
<?php
if(isset($_GET['id']))
{
$dbhostname = ‘localhost’;
$dbusername = ‘root’;
$dbpassword = ”;
$connection = mysql_connect($dbhostname, $dbusername, $dbpassword)
mysql_select_db(“testing”);
$id = $_GET['id'];$sql = “SELECT name, type, size, content FROM tbl_upload WHERE id = ‘$id’”;
$res = mysql_query($sql);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;mysql_close($connection);
exit;}
?>
Before sending the file content using echo first we need to set several headers. They are :
header(“Content-length: $size”) : This header tells the browser how large the file is. it’s a good manner telling how big the file is. so anyone who download the file can predict how much time the download will take.
header(“Content-type: $type”) : This header tells the browser what kind of file it tries to download.
header(“Content-Disposition: attachment; filename=$name”) : Tells the browser to save this downloaded file under the specified name.
After sending the file the script stops executing by calling exit.
Cheers !!
Comments
8 Responses to “Mysql Tutorial – Download files from MYSQL database”
Leave a Reply




I am
March 21st, 2009 @ 3:02 am
[...] That’s it now you can upload your files to MySQL. Now your job is to download the files back from MYSQL database. to view how to download that files back click here. [...]
April 29th, 2010 @ 4:56 am
[...] Download files from MYSQL database [...]
June 25th, 2010 @ 9:28 pm
Oi mate, the line
$connection = mysql_connect($dbhostname, $dbusername, $dbpassword)
requires a semicolon at the end…in both the upload instructions and download instructions…it was a bug for me for 3 days! cheers
ksa
June 25th, 2010 @ 5:28 pm
Oi mate, the line
$connection = mysql_connect($dbhostname, $dbusername, $dbpassword)
requires a semicolon at the end…in both the upload instructions and download instructions…it was a bug for me for 3 days! cheers
ksa
October 13th, 2010 @ 11:19 pm
Hi, am using same code but my pdf file get corrupted when am trying to download it.
Please comment.
- Tushar
May 18th, 2011 @ 2:20 am
$res = mysql_query($sql); list($name, $type, $size, $content) = mysql_fetch_array($result);
$res should be $result
September 13th, 2011 @ 9:46 am
resort chaweng beach koh samui outside bathtub wifi…
[...]Download files from MYSQL database | Online Hungama[...]…
September 17th, 2011 @ 1:41 pm
remove google redirect virus 2011…
[...]Download files from MYSQL database | Online Hungama[...]…