PHP Uploading Multiple Files on a single page

PHP Uploading Multiple Files on a single page.

index.php

Put this code in index.php.

<!DOCTYPE html>
<html>
<head>
	<title>PHP Uploading Multiple Files on a single page </title>
</head>
<body>
	<div style="height:40px;"></div>
	<div style="margin:auto; padding:auto; width:90%;">
		<span style="font-size:25px; color:red"><center><strong>Uploading Multiple Files into MySQL Database on a single page using PHP/MySQLi</strong></center></span>
			
		
		<form method="POST" action="upload.php" enctype="multipart/form-data">
		<input type="file" name="upload[]" multiple>
		<input type="submit" value="Upload"> 
		</form>
	</div>
	<div  width:80%;">
		<h2>Output:</h2>
		<?php
			include('conn.php');
			$query=mysqli_query($conn,"select * from photo");
			while($row=mysqli_fetch_array($query)){
				?>
				<img src="<?php echo $row['location']; ?>" height="150px;" width="150px;">
				<?php
			}
		
		?>
	</div>
</body>
</html>

conn.php

Put this code in conn.php.

<?php
 
//MySQLi Procedural
$conn = mysqli_connect("localhost","root","","upload");
if (!$conn) {
	die("Connection failed: " . mysqli_connect_error());
}
 
?>

upload.php

Put this code in upload.php.

<?php
	include('conn.php');
	
	foreach ($_FILES['upload']['name'] as $key => $name){
		
		$newFilename = time() . "_" . $name;
		move_uploaded_file($_FILES['upload']['tmp_name'][$key], 'upload/' . $newFilename);
		$location = 'upload/' . $newFilename;
		
		mysqli_query($conn,"insert into photo (location) values ('$location')");
	}
	header('location:index.php');
?>

Download Code

PHP Uploading Multiple Files on a single page

Prof.Fazal Rehman Shamil (Available for Professional Discussions)
1. Message on Facebook page for discussions,
2. Video lectures on Youtube
3. Email is only for Advertisement/business enquiries.