I am trying to make a simple file upload from php to mysql and downloading it back but i keep to seem on running into a problem, but I can't figure it out. The picture that I try to upload in this form creates some content in the blob column but on download widows viewer gives and error of no preview available
Here's the code for the form
<form enctype="multipart/form-data" method="post" action="upload.php">
Choose your file <input name="file" type="file">
<input type="submit" >
</form>
Here's the code for upload.php
include('connect.php');
$actualname=$_FILES['file']['name'];
$type=$_FILES['file']['type'];
$name = $_FILES['file']['tmp_name'];
$size = $_FILES['file']['size'];
$fresource=fopen($name,'r');
$content=fread($fresource,filesize($name));;
$content=addslashes($content);
fclose($fresource);
$query='INSERT INTO `files` (Name,Content,Type,Size) VALUES ("'.$actualname.'","'.$content.'","'.$type.'","'.$size.'")';
echo $query;
$var=mysql_query($query,$con);
and here's the code for download.php
include('connect.php');
$query='SELECT * FROM `files` WHERE ID="2"';
$res=mysql_query($query,$con);
$var=mysql_fetch_array($res);
header("Content-length: ".$var[4]);
header("Content-type: ".$var[3]);
header("Content-Disposition: attachment; filename=".$var[1]);
echo $var[1];
Any help would be much appreciated
The files table has the ID,Name,Content,Type,Size columns in the same order
mysql_*functions are deprecated, usemysqli_*orpdo. 2. it's not a good idea to store files in database. You'd better save file in file-system and in database keep reference to it.