PHP sample code to Upload file as BLOB to MySQL Database with download

When it comes to uploading file via a web application, you can choose to upload the file to a folder and just store the file name to a database for easy retrieval, or you can choose to upload the entire file as a BLOB into a Database (as discussed here), the option you opts for depends on the sensitivity or confidentiality of the file in question, if it's just pictures of a user of a web forum or dating site, we can go for the former option (upload file to folder) but if it's company confidential file or military document I will advise it is stored directly into a database as a BLOG.

The sample code below shows the screenshot and the PHP code that allows web user to upload directly into MySQL database as BLOG. This sample is extracted from a larger project I built for the Nigerian Air force.

Download PHP code/db in ZIP




define ('DB_USER', 'root');
define ('DB_PASSWORD', '');
define ('DB_HOST', 'localhost');
define ('DB_NAME', 'filemgr');

$conn = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) or die('Failure: ' . mysql_error() );
mysql_select_db(DB_NAME) or die ('Could not select database: ' . mysql_error() );

error_reporting(E_ERROR | E_PARSE);





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'];
	 $fp = fopen($tmpName, 'r');
	 $content = fread($fp, filesize($tmpName));
	 $content = addslashes($content);
			 $fileName = addslashes($fileName);
		 $query = "INSERT INTO upload_table (name, size, type, content) VALUES ('$fileName', '$fileSize', '$fileType', '$content')";
		 mysql_query($query) or die(mysql_error()); 
		 $msg = "<br>File <b>$fileName</b> uploaded<br>";

<title>PHP File Upload to MySQL Script</title>
<b>PHP sample code to Upload file as BLOB to MySQL Database with download</b><br/>

 	print $msg;



<form method="post" enctype="multipart/form-data">
	 <input type="hidden" name="MAX_FILE_SIZE" value="400000000" />
     <input name="userfile" type="file" id="userfile" />
     <input name="upload" type="submit" class="box" id="upload" value=" Upload " /></td>

$sql = "SELECT id, name FROM upload_table ";
$result = mysql_query($sql) or die(mysql_error());
 print "
<br />

 <table border=1>
	 	<td>File Name</td>
		<td>Download file</td>
	 while($info = mysql_fetch_array($result)){ 
		 $id = $info['id'];
		 $name = $info['name'];
	 print "
	 	<td><a href=\"download.php?id=$id\">download</a></td>		
 print "</table>";

			$id  = mysql_escape_string($_GET['id']);
			$query = "SELECT name, type, size, content " .
					  "FROM upload_table 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;

Download PHP code/db in ZIP

Return to PHP Sample Main Page