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.

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

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

Step 3: File Upload Form HTML

Create an image/file uploading form
HTML form

how to upload multiple image in php and store in database and folder?

Select Image Files to Upload:

Step 4: Upload Multiple Files in PHP

The image uploads the logic script file
do_member.php

$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)?'
'.$profileImgErrStore.'
'.$profileImgErrStoreType:'
'.$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

query("SELECT * FROM members ORDER BY id DESC");

if($query->num_rows > 0){
    while($row = $query->fetch_assoc()){
        $imageURL = 'uploads/'.$row["file_name"];
?>
    

    

Sorry, No image(s) found...

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.

Leave a Comment