This tutorial i am explain about to how to upload files to MYSQL Database and Download files from MYSQL data.
Upload.php
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Upload files to database</title>
<style>
.san
{
width:700px;
height:auto;
margin-left:auto;
margin-right:auto;
padding:20px;
border:1px solid #CCC;
border-radius:20px;
}
</style>
</head>
<body>
<div class="san">
<form method="post" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1"
cellspacing="1" class="box">
<tr>
<td><h1 style="text-decoration:underline;">Select Your File</h1></td></tr>
<tr>
<td>
<input type="hidden" name="MAX_FILE_SIZE"
value="16000000">
<input name="userfile" type="file" id="userfile">
</td>
<td width="80"><input name="upload"
type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>
<?php
if(isset($_POST['upload'])&&$_FILES['userfile']['size']>0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$fileType=(get_magic_quotes_gpc()==0 ? mysql_real_escape_string(
$_FILES['userfile']['type']) : mysql_real_escape_string(
stripslashes ($_FILES['userfile'])));
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
$con = mysql_connect('localhost', 'root', '') or die(mysql_error());
$db = mysql_select_db('sanwebcorner', $con);
if($db){
$query = "INSERT INTO upload (name, size, type, content ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";
mysql_query($query) or die('Error, query failed');
mysql_close();
echo "<br>File $fileName uploaded<br>";
}else { echo "file upload failed"; }
}
?>
<a href="download.php">Click here to download uploaded files</a>
</div>
</body>
</html>
Download.php
<html>
<head>
<title>Download Files From MySQL Database</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<style>
.san
{
width:700px;
height:auto;
margin-left:auto;
margin-right:auto;
padding:20px;
border:1px solid #CCC;
border-radius:20px;
}
</style>
</head>
<body>
<div class="san">
<h1 style="text-decoration:underline; color:#476804; font-weight:bold;">Download files you may download now.....</h1>
<?php
//database connection
$con = mysql_connect('localhost', 'root', '') or die(mysql_error());
//select database
$db = mysql_select_db('sanwebcorner', $con);
$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=<?php echo urlencode($id);?>"
><?php echo urlencode($name);?></a> <br>
<?php
}
}
mysql_close();
?>
</div>
</body>
</html>
<?php
if(isset($_GET['id']))
{
// if id is set then get the file with the id from database
$con = mysql_connect('localhost', 'root', '') or die(mysql_error());
$db = mysql_select_db('sanwebcorner', $con);
$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");
ob_clean();
flush();
echo $content;
mysql_close();
exit;
}
?>
0 Comments