how to upload multiple images in php and store in database?

how to upload multiple images in php and store in database – The functionality is also handled to insert uploaded images detail into MySQL database table. It’s handle to upload multiple images in php mysql.

multiple image upload in php code with databases demo

Create an HTML form to select multiple images and files. as well as Show multiple images preview before sending to server.

Step 1: Create Database Table

how to upload multiple image in php and store in database and folder?
SQL creates an images table
Create a database table

CREATE TABLE `members` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `file_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
 `uploaded_on` datetime NOT NULL,
 `status` enum('1','0') COLLATE utf8_unicode_ci NOT NULL DEFAULT '1',
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Step 2: Database Configuration (databaseConnection.php)

how to upload multiple images in php and store in database? – The databaseConnection.php file is used to step by step connect and select the MySQL database using PHP. Specify the database hostname Like as “pakainfo_v1” ($dbHost), username Ex: root ($dbUsername), password ($dbPassword), as well as set the name ($dbName) as per your MySQL details.
databaseConnection.php
The database connection file.

<?php

$dbHost     = "localhost";
$dbUsername = "root";
$dbPassword = "[email protected]";
$dbName     = "pakainfo_v1";


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

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

Don’t Miss : multiple image upload in php code with databases demo

Read Also:  Make Bootstrap Modal Fullscreen

Step 3: File Upload Form HTML

Create an image/file uploading form
HTML form

<h2>how to upload multiple image in php and store in database and folder?</h2>
<form action="do_member.php" method="post" enctype="multipart/form-data">
    Select Image Files to Upload:
    <input type="file" name="files[]" multiple >
    <input type="submit" name="submit" value="UPLOAD">
</form>

Step 4: Upload Multiple Files in PHP

The image uploads the logic script file
do_member.php

<?php 
include_once 'databaseConnection.php'; 

//how to insert multiple images in php     
if(isset($_POST['submit'])){ 

    $targetDir = "uploads/"; 
    $fileFormat = array('jpg','png','jpeg','gif'); 
     
    $displayMessage = $messageErr = $storeMembersInfo = $profileImgErrStore = $profileImgErrStoreType = ''; 
    $fileNames = array_filter($_FILES['files']['name']); 
    if(!empty($fileNames)){ 
        foreach($_FILES['files']['name'] as $key=>$val){ 

            $fileName = basename($_FILES['files']['name'][$key]); 
            $basePath = $targetDir . $fileName; 
             

            $fileType = pathinfo($basePath, PATHINFO_EXTENSION); 
            if(in_array($fileType, $fileFormat)){ 

                if(move_uploaded_file($_FILES["files"]["tmp_name"][$key], $basePath)){ 

                    $storeMembersInfo .= "('".$fileName."', NOW()),"; 
                }else{ 
                    $profileImgErrStore .= $_FILES['files']['name'][$key].' | '; 
                } 
            }else{ 
                $profileImgErrStoreType .= $_FILES['files']['name'][$key].' | '; 
            } 
        } 
         
 
        $profileImgErrStore = !empty($profileImgErrStore)?'Upload Error: '.trim($profileImgErrStore, ' | '):''; 
        $profileImgErrStoreType = !empty($profileImgErrStoreType)?'File Type Error: '.trim($profileImgErrStoreType, ' | '):''; 
        $messageErr = !empty($profileImgErrStore)?'<br/>'.$profileImgErrStore.'<br/>'.$profileImgErrStoreType:'<br/>'.$profileImgErrStoreType; 
         
        if(!empty($storeMembersInfo)){ 
            $storeMembersInfo = trim($storeMembersInfo, ','); 

            $insert = $link->query("INSERT INTO members (file_name, uploaded_on) VALUES $storeMembersInfo"); 
            if($insert){ 
                $displayMessage = "Good Luck, Files are uploaded successfully.".$messageErr; 
            }else{ 
                $displayMessage = "Sorry, there was an error uploading your file."; 
            } 
        }else{ 
            $displayMessage = "Upload failed! ".$messageErr; 
        } 
    }else{ 
        $displayMessage = 'Please select a file to upload.'; 
    } 
} 
 
?>

Step 5: Display Images from Database

index.php

<?php
//multiple image upload in php code with databases
include_once 'databaseConnection.php';

$query = $link->query("SELECT * FROM members ORDER BY id DESC");

if($query->num_rows > 0){
    while($row = $query->fetch_assoc()){
        $imageURL = 'uploads/'.$row["file_name"];
?>
    <img src="<?php echo $imageURL; ?>" alt="" />
<?php }
}else{ ?>
    <p>Sorry, No image(s) found...</p>
<?php } ?> 

I hope you get an idea about how to upload multiple images in php and store in database.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.