PHP Upload file or picture to folder


The sample code below shows the screenshot and the PHP code that allows web user to upload to folder. For sample on how to upload to database table using BLOB click here

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

?>	

php_upload_to_folder.php

<?php
include("conn.php");

if($_POST['action'] == "do_upload" ) { 

		//generate random value use to rename a file, if already existing in the folder. 		
		function randomizer($length) {
			$arr = array("1","2","3","4","5","6","7","8","9","0","Q","W","E","R","T","Y","U",
							"I","O","P","A","S","D","F","G","H","J","K","L","Z","X",
							"C","V","B","N","M");
			srand((float) microtime() * 1000000);
			for($i = $length; $i > 0; $i--) {
				$str .= $arr[rand(0, sizeof($arr))];        
			}
			return $str;
		}
		$randon_num_generated = randomizer(5);


							//piks is the folder where the pictures will be store when uploaded
							$uploaddir = "piks/";
							
							 
							 
							// get files temporary name 
							$file = $_FILES['userfile']['tmp_name']; 
							 
							// get files actual name
							$filename = $_FILES['userfile']['name']; 
								 
							// Check to see if the file allready exists? 
							if(file_exists($uploaddir . $filename)) { 
							
								// rename the file if exist by generating randon unique values ()
								$filename = $randon_num_generated."_".$filename;
								
								// copy file.
								$fn = $filename;										
								copy($file, $uploaddir.$filename) or die("Could not copy file.");									
								
							} else { 
								// copy file. 								
								$fn = $filename;
								copy($file, $uploaddir.$filename) or die("Could not copy file."); 
							} 
		
		//insert file name into the database
		$add = "INSERT INTO upload_file VALUE ('', '$fn')";
		mysql_query($add, $conn) or die(mysql_error());	
					

		$msg = "Your upload was Successful!";
		
}		
?>

<head>
<title>PHP File Upload to MySQL Script</title>
</head>
<body>
<b>PHP sample code to Upload file to folder</b>
<br/>

<form action="php_upload_to_folder.php" method="post" enctype="multipart/form-data">
    Select file to upload<input name="userfile" type="file" id="userfile" />
                
          <input type="hidden" name="action" value="do_upload" />
		  <input type="submit" name="Submit" value="Upload Now"/> 
        
<?php
if(isset($msg)){
	print "$msg";
}
?>    
</form>

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

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

Download PHP code/db in ZIP

Return to PHP Sample Main Page