Multiple image upload concept using php & mysql - Mostlikers

04 March, 2014

Multiple image upload concept using php & mysql

In this post, i have explained about How to upload multiple images in the database. Here I have used upload multiple images in PHP and MySQLi scripts. This tutorial will make multiple uploads with the verification for image type, image size, image extension codes with a live demo are there. 

multiple image


Live Demo          Download

Post update :  MySQL deprecated  so, I have changed this code multiple image upload in PHP code with databases to MySQLi


Database

Create sample table name called 'gallery' and create id,image column name. 
CREATE TABLE IF NOT EXISTS `gallery` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `image` varchar(250) NOT NULL,
  PRIMARY KEY (`id`)
)

Config.php

connect database using MySQLi code. change db_name to your database name.
<?php
$db_username    = 'root';
$db_password    = '';
$db_name        = 'db_name';
$db_host        = 'localhost';
$db = new mysqli($db_host, $db_username, $db_password,$db_name);
?>


Index.php

Here I have created HTML form file upload multiple select attributes.
images[] - collect the multiple with array form.
accept="image/*" - While browsing the file it will show only image kind of files. 
<html>
<body>
  <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>
</body>
</html>

Upload.php

Before use, this code creates one new folder name called uploads in your local directory.
$path ='uploads/' - files will move on uploads folder.
filesize - get uploaded image specify size.     
<?php
include('config.php');
error_reporting(0);     
if(isset($_FILES['images']['name'])):
  define ("MAX_SIZE","2000");
  for($i=0; $i<count($_FILES['images']['name']); $i++)  {
  $size=filesize($_FILES['images']['tmp_name'][$i]);    
  if($size < (MAX_SIZE*1024)):    
   $path = "uploads/";
   $name = $_FILES['images']['name'][$i];
   $size = $_FILES['images']['size'][$i];
   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;
    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 ='Data not inserting';
       endif;
    else :
        $error = 'File moving unsuccessful';
    endif;
  else:
     $error = 'You have exceeded the size limit!';
  endif;      
  }
else:
  $error = 'File not found!';
endif;          
?>
<h2><?php echo @$error ?></h2> <a href="index.php">Try Again</a>

View

Here I have written how to fetch multiple images from database code. It's normal to view code I have used to display the images.  
<html>
<body>
<?php include('config.php');    
  $sql = $db->query("SELECT * FROM gallery");
    if(isset($sql) && count($sql)) :  
      foreach ($sql as $key => $row) : ?>    
       <img src="uploads/<?php echo $row['image']; ?>" /> 
<?php endforeach;   endif; ?>
</body>
</html>


Have a Question? Share your feedback below.

"Excellence is a continuous process and not an accident. "
                                             
                                         - A. P. J. Abdul Kalam








Related Topics

17 comments:

  1. But why is the database still empty? though its working

    ReplyDelete
  2. thanks for for the code but when it did not store the all image path. it only store the last image name not path.

    ReplyDelete
    Replies
    1. check your file type name array format image[]

      $image_path=$filename.$path;
      $fetch=$db->query("INSERT INTO gallery(image) VALUES('$image_path')");

      Delete
  3. I also want to insert path in database

    ReplyDelete
  4. But the form is allowing to upload one image at a time and overwrites the previous if loaded multiple images

    ReplyDelete
    Replies
    1. faced the same situation and no data is inserted in the database even though the connection works

      Delete
  5. hey bro its only store 1 images in database.....how we can store multiple images guide us.

    ReplyDelete
    Replies
    1. The above code will work multiple image.

      Delete
  6. bro how about edit them? guide me please
    thanks in advance

    ReplyDelete
  7. POST Content-Length of 11760096 bytes exceeds the limit of 8388608 bytes in Unknown on line 0

    ReplyDelete
    Replies
    1. 8388608 bytes is 8M, the default limit in PHP. Update your post_max_size in php.ini to a larger value.
      upload_max_filesize sets the max file size that a user can upload while post_max_size sets the maximum amount of data that can be sent via a POST in a form.

      Delete