Upload Multiple Image files to the Database using PDO – PHP

You can store the image files in the Database table either in base64 format or its path after uploading.

In the base64 method, it is not necessary to store the uploaded file on the server because it is been directly accessed with the base64 encoded value.

In this tutorial, I am using PDO connection for storing multiple image files path in the MySQL database table.

Upload Multiple Image files to the Database using PDO - PHP


  1. Table structure
  2. Configuration
  3. HTML
  4. PHP
  5. Conclusion

1. Table structure

I am using images table for storing data.

CREATE TABLE `images` (
  `name` varchar(80) NOT NULL,
  `image` varchar(80) NOT NULL

2. Configuration

Create a config.php file for the database connection.

Completed Code

$server = "localhost";
$username = "root";
$password = "";
$dbname = "tutorial";

// Create connection
  $conn = new PDO("mysql:host=$server;dbname=$dbname","$username","$password");
}catch(PDOException $e){
  die('Unable to connect with the database');


Create a <form> within this add a file element and a submit button. For enable selecting multiple files define multiple attribute in file element.

Completed Code

<form method='post' action='' enctype='multipart/form-data'>
  <input type='file' name='files[]' multiple />
  <input type='submit' value='Submit' name='submit' />

4. PHP

Create an upload directory for storing the image files.

On the submit button click count total selected files and create a prepared statement for inserting the record in the images table.

Loop on the files and extract the extension to check file is image or not.

If it is image file then stores it in upload directory and execute the statement using execute() method where pass an array while contains file name and path.

Completed Code

include "config.php";

  // Count total files
  $countfiles = count($_FILES['files']['name']);
  // Prepared statement
  $query = "INSERT INTO images (name,image) VALUES(?,?)";

  $statement = $conn->prepare($query);

  // Loop all files

    // File name
    $filename = $_FILES['files']['name'][$i];

    // Get extension
    $ext = end((explode(".", $filename)));

    // Valid image extension
    $valid_ext = array("png","jpeg","jpg");

    if(in_array($ext, $valid_ext)){

      // Upload file

        // Execute query


  echo "File upload successfully";

5. Conclusion

In the demonstration, I stored uploaded image files path in the MySQL database table when file successfully uploaded but you can also store the file in the base64 format in the field.

If you found this tutorial helpful then don't forget to share.
Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request.
Spread the love
  • 7


  1. Marco said:

    Thanks a lot! Made my day ….

    January 15, 2018
  2. Jason said:

    Can this script work for PDFs?

    May 29, 2018
    • Yogesh Singh said:

      Yes, it works. Just need to modify the upload script inside the loop on form submit.

      May 29, 2018
  3. sunny said:

    can this work with mysql workbench

    July 1, 2018

Leave a Reply

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