Multiple Image upload in PHP and MySQL

  • Tech Area
  • December 13, 2023



In this tutorial, We will learn how to upload multiple image in PHP and MySQL.

Files used in this tutorial:

1- connection.php (database connection file)

2- index.php (multiple image upload file)

Below are the step by step process of how to upload multiple image using PHP and MySQL.

Step 1: Create Database and Table

Create a database with name college_db. Now create a table with name tbl_data in the college_db database.

Sample syntax of table tbl_data.

CREATE TABLE `tbl_data` (
`id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`image` text NOT NULL, 
`created_at` timestamp
)DEFAULT CHARSET=latin1;

Step 2: Create a Database connection

In this step, create a new file connection.php to create database connection.

connection.php

<?php
$server = "localhost";
$username = "root";
$password = "";
$database = "college_db";
$connection = mysqli_connect("$server","$username","$password");
$select_db = mysqli_select_db($connection, $database);
if(!$select_db)
{
	echo("connection terminated");
}
?>

Step 3: Create index.php

In this step, create a new file index.php. This is the main file used to create an HTML form for multiple select file and upload into the database.

This screenshot shows an HTML form.

index.php

<html>  
<head>  
    <title>Multiple Image Upload Form</title>  
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
</head>
<style>
 .box
 {
  width:100%;
  max-width:600px;
  background-color:#f9f9f9;
  border:1px solid #ccc;
  border-radius:5px;
  padding:16px;
  margin:0 auto;
 }
 .error
{
  color: red;
  font-weight: 700;
} 
</style>
<?php
include('connection.php');
if(isset($_REQUEST['img-upload']))
{
  for($i=0; $i<count($_FILES['multiple_image']['name']); $i++)
  {
    $filename[] = basename($_FILES['multiple_image']['name'][$i]);
    $uploadfile = $_FILES['multiple_image']['tmp_name'][$i];
    $targetpath = "uploads/".$filename[$i];
    move_uploaded_file($uploadfile, $targetpath);
  }
    $images = implode(', ',$_FILES['multiple_image']['name']);
    $insert_query = mysqli_query($connection,"insert into tbl_data set image='$images'");
    if($insert_query>0)
    {
      $msg = "Images uploaded successfuly";
    }
    else
    {
      $msg = "Error!";
    }
}
?>
<body>  
    <div class="container">  
    <div class="table-responsive">  
    <h3 align="center">Multiple Image Upload Form</h3><br/>
    <div class="box">
     <form method="post" enctype="multipart/form-data">
     <div class="form-group">
       <label for="image">Select Multiple Image</label>
       <input type="file" name="multiple_image[]" class="form-control" multiple required/>
      </div>  
      <div class="form-group">
       <input type="submit" id="img-upload" name="img-upload" value="Submit" class="btn btn-success" />
       </div>
       <p class="error"><?php if(!empty($msg)){ echo $msg; } ?></p>
     </form>
     </div>
   </div>  
  </div>
 </body>  
</html>

Download Source Code


Subscribe us via Email

Join 10,000+ subscriber

Subscribe on YouTube