PHP MySQL Multiple Image Upload Example

Mar 04, 2022 . Admin

Hello Friends,

I am going to explain you upload multiple image in PHP MySQL Database example. You will learn how to multiple upload image in db using PHP. In side this article we will see how to multiple upload images in PHP with Database.

This article will give you simple example of PHP MySQL multiple upload image. We will use get simple PHP code for multiple upload image in Database.

You can use from how to multiple upload image in Database using PHP and MySQL. I will give you simple how to PHP multiple upload photo to Database.

So, let's see bellow solution:

<!DOCTYPE html>
<html lang="en-US">
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>PHP MySQL Multiple Image Upload -</title>
    <link rel="stylesheet" href="">
        $dbHost     = "localhost";
        $dbUsername = "root";
        $dbPassword = "";
        $dbName     = "storage";

        $db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);

        if ($db->connect_error) {
            die("Connection failed: " . $db->connect_error);

            $targetDir = "upload/";
            $allowTypes = array('jpg','png','jpeg','gif');
            $images_arr = array();

            foreach($_FILES['images']['name'] as $key=>$val){
                $image_name = $_FILES['images']['name'][$key];
                $tmp_name = $_FILES['images']['tmp_name'][$key];
                $size = $_FILES['images']['size'][$key];
                $type = $_FILES['images']['type'][$key];
                $error = $_FILES['images']['error'][$key];
                $fileName = basename($_FILES['images']['name'][$key]);
                $targetFilePath = $fileName;
                $fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);

                if(in_array($fileType, $allowTypes)){   
                        $images_arr[] = $targetFilePath;
                        $insert = $db->query("INSERT into images (image_name) VALUES ('".$targetFilePath."')");

                            $count = $key + 1;
                            $success = " ".$count. " image file has been uploaded successfully.";
                            $statusMsg = "Failed to upload image";
                        $statusMsg = "Sorry, there was an error uploading your file.";
                    $statusMsg = 'Please Upload a Valid File';

    <div class="container mt-5">
        <div class="row">
            <div class="col-md-12">
                <div class="card w-75 m-auto">
                    <div class="card-header text-center bg-danger text-white">
                        <h3>PHP MySQL Multiple Image Upload -</h3>
                    <div class="card-body">
                        <form action="" method="post" enctype="multipart/form-data">

                            <?php if(!empty($success)){ ?>
                                <p class="alert alert-success"><?php echo $success; ?></p>
                            <?php } ?>

                            <div class="form-group">
                                <div class="custom-file">
                                    <label><strong>Select Images : <span class="text-danger">*</span></strong></label>
                                    <input type="file" name="images[]" id="customFile" class="form-control" multiple required>

                                <?php if(!empty($statusMsg)){ ?>
                                    <p class="text-danger"><?php echo $statusMsg; ?></p>
                                <?php } ?>


                            <div class="form-group d-flex justify-content-center">
                                <input type="submit" name="submit" value="Upload" class="btn btn-success rounded-0 px-3">

It will help you...