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:

index.php
<!DOCTYPE html>
<html lang="en-US">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>PHP MySQL Multiple Image Upload - Mywebtuts.com</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>
    <?php 
        $dbHost     = "localhost";
        $dbUsername = "root";
        $dbPassword = "";
        $dbName     = "storage";

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

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

        if(isset($_POST['submit'])){
            $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)){   
                    if(move_uploaded_file($_FILES['images']['tmp_name'][$key],$targetFilePath)){
                        $images_arr[] = $targetFilePath;
                
                        $insert = $db->query("INSERT into images (image_name) VALUES ('".$targetFilePath."')");

                        if($insert){
                            $count = $key + 1;
                            $success = " ".$count. " image file has been uploaded successfully.";
                        }else{
                            $statusMsg = "Failed to upload image";
                        } 
                    }else{
                        $statusMsg = "Sorry, there was an error uploading your file.";
                    }
                }else{
                    $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 - Mywebtuts.com</h3>
                    </div>
                    <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>
                                </div>

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

                            </div>

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

It will help you...
#PHP