Main.php
<title> File Upload and Download (MYSQL Database)</title>
<h2 align="center">File Upload and Download (MYSQL Database)</h2>
<table width="200" border="0" align="center" >
<tr id="trr">
<td align="center"><a href="upload.php?up=yes"><strong>
Upload File
</strong> </a> </td>
</tr>
<tr id="trr" >
<td align="center"><a href="download.php?dw=yes"><strong>
Download File
</strong> </a></td>
</tr>
</table>
upload.php
<title>Upload Files to MYSQL Database</title>
<h2 align="center">Upload Files to MYSQL Database</h2>
<?php
if($_GET['up']=="yes")
{
print '<form name="uplform" action="" method="post" enctype="multipart/form-data" >
<table width="450" border="0" align="center" ><stron>
<tr id="trr">
<th width="207">File Upload </th>
<td width="233"><input type="file" name="uplfile" value="sss"></td></tr>
<tr id="trr">
<td> </td>
<td> </td>
</tr>
<tr id="trr">
<td align="right"><input type="submit" name="Submit" value="Submit"></td>
<td><input type="reset" name="Reset" value="Reset"></td>
</tr></strong>
</table></form>';
}
?>
<?php
if(isset($_POST['Submit']) && $_POST['Submit']=="Submit")
{
$flag=0;
if(isset($HTTP_POST_FILES['uplfile']))
{
if(is_uploaded_file($HTTP_POST_FILES['uplfile']['tmp_name']))
if(!file_exists($HTTP_POST_FILES['uplfile']['name']))
if(@rename($HTTP_POST_FILES['uplfile']['tmp_name'],$HTTP_POST_FILES['uplfile']['name']))
$flag=1;
else
$flag=0;
}
if($flag==1)
{
$file_u=$HTTP_POST_FILES['uplfile']['name'];
$da=date("Y-m-d ",time() + (34200)).date("H:i:s",time() + (34200));
mysql_connect("localhost","root","") or die("Sever connection error");
mysql_select_db("file") or die ("Database error");
$val = fread(fopen($file_u, "r+"), filesize($file_u)+1);
if (!empty($val))
{
$val = '0x' . bin2hex($val);
$ext=substr($HTTP_POST_FILES['uplfile']['name'],strlen($HTTP_POST_FILES['uplfile']['name'])-3,3);
$q1="INSERT INTO `file_tb` ( `id` , `file_name` , `file` , `ext` , `date_time` )
VALUES ( NULL , '$file_u', $val, '$ext', '$da')";
if(@mysql_query($q1))
print '<h1 align="center">File Successfully Uploaded</h1>';
else
print '<h1 align="center">File is too Large</h1>';
}
$do=unlink ($HTTP_POST_FILES['uplfile']['name']);
}
}
?>
download.php
<title>Download Files From MYSQL Database</title>
<h2 align="center">Download Files From MYSQL Database</h2>
<?php
if($_GET['dw']=="yes")
{
print '
<table border="0" align="center" width="500">
<tr id="thr">
<th>File ID</th>
<th>File Name</th>
<th>File Ext</th>
<th>Date & Time</th>
</tr>';
mysql_connect("localhost","root","") or die("Sever connection error");
mysql_select_db("file") or die ("Database error");
$sel=mysql_query("select * from file_tb") or die("Table Error");
while($r=mysql_fetch_array($sel))
{
print '<tr id="trr">';
print '<td>'.$r['id'].'</td>';
print '<td><a href="download.php?action=view&id='.$r['id'].'">'.$r['file_name'].'</a></td>';
print '<td>'.$r['ext'].'</td>';
print '<td>'.$r['date_time'].'</td>';
print '</tr>';
}
print '</table>';
}
?>
<?php
if (isset($_GET['action']) && ($_GET['action'] == "view"))
{
$fid=$_GET['id'];
mysql_connect("localhost","root","") or die("Sever connection error");
mysql_select_db("file") or die ("Database error");
$sel=mysql_query("select * from file_tb") or die("Table Error");
while($r=mysql_fetch_array($sel))
{
if($r['id']==$fid)
{
$fi=$r['file'];
$ab="files/".$r['file_name'];
$fp=fopen($ab,"w+") or die("file error");
fwrite($fp,$fi);
print '<img src="';
print $ab;
print '" >';
print '<h3 align="center">File Downloaded in <a href="files">Files</a> Directory</h3>';
}
}
}
?>
DATABASE.php
<title>MYSQL Table Installation</title>
<?php
print '<h2 align="center">MYSQL Table Installation</h2>';
mysql_connect("localhost","root","") or die("Sever connection error");
mysql_query("CREATE DATABASE IF NOT EXISTS file") or die("Database Already Exists");
mysql_select_db("file");
$q="CREATE TABLE IF NOT EXISTS `file_tb` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`file_name` VARCHAR( 20 ) NOT NULL ,
`file` LONGBLOB NOT NULL ,
`ext` VARCHAR( 5 ) NOT NULL ,
`date_time` DATETIME NOT NULL
) ENGINE = MYISAM
";
mysql_query($q) or die("Table Error");
print '<h3 align="center">Installation Complete</h3>';
?>
first run database.php, then run main.php.