Multiple Image Upload using PHP into MYSQL database

Multiple Image Upload using PHP into MYSQL database

Today, We want to share with you Multiple Image Upload using PHP into MYSQL database.
In this post we will show you Multiple Image Upload using PHP into MYSQL database, hear for Multiple Image Upload using PHP into MYSQL database we will give you demo and example for implement.
In this post, we will learn about Multiple Image Upload using PHP into MYSQL database with an example.

i have This IMP POST explained about How to upload The multiple images or Files in the database(Like as a Mysql).

The Multiple all files or image can be uploaded using different name for HTML input.

enctype=”multipart/form-data”. 
 type=” file”

upload multiple images in php example

Use 
 <form action="do_upload.php" method="post" enctype="multipart/form-data">
    <input type="file"  name="images[]"  multiple="multiple" accept="image/*" />
    <input type="submit" name="sub" value="Upload!" />
 </form>

“Multiple Image Upload” using PHP into MYSQL database

File Structure

multiple_upload(Dir Name)
->upload(Dir Name)
->config.php
->index.php
->upload.php

Also Read :  Top 10 Advanced MySQL Interview Questions Answers

DataBase Name : gallery_tbl

First of All Crate a Simple TABLE in Your DataBase.

Table Name : gallery

CREATE TABLE IF NOT EXISTS `gallery` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `image` varchar(250) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

File Name : Config.php

<?php
$db = new mysqli("localhost","root","mypass**","gallery_tbl");
// Check It My SQLi connection
if (mysqli_connect_errno())
{
	echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?> 

File Name : index.php

<html>
<head><title>How to upload/store multiple images in mysql database in php</title></head>
<body>
<div style="margin:auto;width:900px;">

<div>
 <form action="upload.php" method="post" enctype="multipart/form-data">
    <input type="file"  name="images[]"  multiple="multiple" accept="image/*" />
    <input type="submit" name="sub" value="Upload!" />
 </form>
</div>
<?php 
  //include config.php where all configuration
  include('config.php');	
  
  //execute select query for fetch database to images name
  $sql = $db->query("SELECT * FROM gallery");
  if(isset($sql) && count($sql)) :  // check condition
  foreach ($sql as $key => $row) : ?>
  //print all display all images
   <img class="circle" src="uploads/<?php echo $row['image']; ?>" /> 
<?php   endforeach;   endif; ?>

</div>
</body>
</html>
<style type="text/css">
  .circle {
  border: solid 1px #dedede;
  width: 250px;
  padding: 4px;
  background-color: #fff;
  border-radius: 70%;
  -webkit-transition: -webkit-transform .8s ease-in-out;
  transition: transform .8s ease-in-out;
}
.circle:hover {
  
  -webkit-transform: rotate(360deg);
  transform: rotate(360deg);
}
  </style>

File Name : upload.php

<?php
include('config.php');
error_reporting(0);	
//check condition for images upload or not	
if(isset($_FILES['images']['name'])):
  define ("MAX_SIZE","2000");
  //all multiple images fetch using for loop
  for($i=0; $i<count($_FILES['images']['name']); $i++)  {
  // check size for images
  $size=filesize($_FILES['images']['tmp_name'][$i]);    
  
  //check condition for image size
  if($size < (MAX_SIZE*1024)):    
   $path = "uploads/"; // upload directory file name
   $name = $_FILES['images']['name'][$i]; // image name store
   $size = $_FILES['images']['size'][$i]; // image size store
   list($txt, $ext) = explode(".", $name);
   date_default_timezone_set ("Asia/Calcutta"); 
   $currentdate=date("d M Y");	
   $file= time().substr(str_replace(" ", "_", $txt), 0);
   $info = pathinfo($file);
   $filename = $file.".".$ext;// all images uploaded here
    if(move_uploaded_file($_FILES['images']['tmp_name'][$i], $path.$filename)) : 	      
	   $fetch=$db->query("INSERT INTO gallery(image) VALUES('$filename')");
	   if($fetch):
	   	 header('Location:index.php');
	   else :
         $error ='your Data not inserting';
	   endif;
	else :
		$error = 'your File (All images) moving unsuccessful';
	endif;
  else:
  	 $error = 'You have exceeded the File (some images or files)size limit!';
  endif;      
  }
else:
  $error = 'Your File not found!';
endif;			
?>

View Demo

Also Read :  Advanced jQuery Ajax Pagination PHP MySQL using jQuery

We hope you get an idea about Multiple Image Upload using PHP into MYSQL database
We would like to have feedback on my Information blog .
Your valuable any feedback, Good question, Inspirational Quotes, or Motivational comments about this article are always welcome.
If you liked this post, Please don’t forget to share this as Well as Like FaceBook Page.

We hope This Post can help you…….Good Luck!.

Leave a Reply

Your email address will not be published. Required fields are marked *