How to upload multiple images using PDO

To upload multiple images using PHP's PDO (PHP Data Objects), you can follow these steps. In this example, I'll show you how to create an HTML form for uploading multiple images and use PHP with PDO to handle the file uploads and database insertion.

Step 1: Create the HTML Form

Create an HTML form that allows users to select and upload multiple image files. Use the multiple attributes in the file input to allow multiple file selection.

<!DOCTYPE html>
    <title>Multiple Image Upload</title>
    <h2>Upload Multiple Images</h2>
    <form action="upload.php" method="post" enctype="multipart/form-data">
        <input type="file" name="images[]" multiple="multiple">
        <input type="submit" value="Upload Images" name="submit">

Step 2: Create the PHP Script for Uploading Images (upload.php)

Create a PHP script (upload.php) to handle the file uploads and database insertion. Make sure to create a table in your database to store information about the uploaded images.

// Database connection
try {
    $pdo = new PDO('mysql:host=localhost;dbname=your_database', 'your_username', 'your_password');
} catch (PDOException $e) {
    die("Database connection failed: " . $e->getMessage());

// Function to handle file uploads and insert records into the database
function uploadImages($pdo, $files) {
    $imageFolder = 'uploads/'; // Set your image upload folder path

    foreach ($files['name'] as $key => $name) {
        $tmp_name = $files['tmp_name'][$key];
        $imagePath = $imageFolder . $name;

        // Move uploaded file to destination folder
        if (move_uploaded_file($tmp_name, $imagePath)) {
            // Insert record into the database
            $stmt = $pdo->prepare("INSERT INTO images (image_path) VALUES (?)");
        } else {
            echo "Failed to upload $name.<br>";

if (isset($_POST['submit'])) {
    if (!empty($_FILES['images']['name'][0])) {
        uploadImages($pdo, $_FILES['images']);
        echo "Images uploaded successfully!";
    } else {
        echo "Please select at least one image to upload.";

In the script above, replace your_database, your_username, and your_password with your database credentials. Also, set the correct folder path in $imageFolder where you want to store the uploaded images.

Step 3: Create the Database Table

Create a database table to store information about the uploaded images. Here's an example SQL query to create the table:

    image_path VARCHAR(255) NOT NULL

Step 4: Create a Folder for Uploaded Images

Create a folder in your project directory where the uploaded images will be stored. In the example above, the folder name is 'uploads'. Make sure this folder has the necessary write permissions.


Step 5: Style and Enhance

You can add error handling, validation, and additional functionality as needed. Additionally, you can display the uploaded images or create a gallery using the stored image paths.

With these steps, you can upload multiple images using PDO in PHP. The uploaded image paths are inserted into the database, making it easy to manage and display them as needed.

Muhammad Alfaiz
Alfaiz 3 months ago
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x