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

 

conn.php

<?php

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);

?>	

upload.php

<?php

include("conn.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'];
	
	 $fp = fopen($tmpName, 'r');
	 $content = fread($fp, filesize($tmpName));
	 $content = addslashes($content);
	 fclose($fp);
	
		 if(!get_magic_quotes_gpc())
		 {
			 $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>";
		
 } 


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

<?php
 if(isset($msg)){
 	print $msg;
 }

?>

<br/>

<form method="post" enctype="multipart/form-data">
Upload
	 <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>
</form>
<hr>

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

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

?>
 
</body>
</html>
  
download.php
<?php
 if(isset($_GET['id'])) 
 {
						
	include("conn.php");
			
			$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;
			
					
			exit;
 }
?>


Download PHP code/db in ZIP

Return to PHP Sample Main Page