Upload Multiple Images and Store in Database Using PHP and MySQL - 07069564369
Upload Multiple Images and Store in Database Using PHP and MySQL - 07069564369
menu search
XM™ Invest in Th
Enjoy Excellent Trading Con
Commissions.
XM
HOME / PHP / UPLOAD MULTIPLE IMAGES AND STORE IN DATABASE USING PHP AND MYSQL
Share
keyboard_double_arrow_up
https://www.codexworld.com/upload-multiple-images-store-in-database-php-mysql/ 1/14
9/7/23, 1:39 AM Upload Multiple Images and Store in Database using PHP and MySQL - CodexWorld
menu search
File upload in PHP is the most used functionality for the web application. A single file or multiple files
can be easily uploaded using PHP. PHP provides a quick and simple way to implement server-side file
upload functionality. Generally, in the web application, the file is uploaded to the server and the file
name is stored in the database. Later the files are retrieved from the server based on the file name
stored in the database.
In most cases, a single image is uploaded at once. But sometimes you have a requirement to upload
multiple images at once. In this tutorial, we will show you how to upload multiple images in PHP and
store the images in the MySQL database. Multiple image upload allows the user to select multiple files
at once and upload all files to the server in a single click.
Our example code will implement the following functionality to demonstrate the multiple images
upload in PHP.
Retrieve images from the database and display on the web page.
keyboard_double_arrow_up
Database Configuration (dbConfig.php)
https://www.codexworld.com/upload-multiple-images-store-in-database-php-mysql/ 2/14
9/7/23, 1:39 AM Upload Multiple Images and Store in Database using PHP and MySQL - CodexWorld
menu
The dbConfig.php file is used to connect and select the MySQL database. Specify the database
hostname ( $dbHost ), username ( $dbUsername ), password ( $dbPassword ), and name ( $dbName ) as search
per your MySQL credentials.
<?php
// Database configuration
$dbHost = "localhost";
$dbUsername = "root";
$dbPassword = "root";
$dbName = "codexworld";
// Create database connection
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
// Check connection
if ($db->connect_error) {
die("Connection failed: " . $db->connect_error);
}
?>
method="post"
enctype="multipart/form-data"
Include the database configuration file to connect and select the MySQL database.
Get the file extension using pathinfo() function in PHP and check whether the user selects only the
image files. keyboard_double_arrow_up
Upload images to the server using move_uploaded_file() function in PHP.
https://www.codexworld.com/upload-multiple-images-store-in-database-php-mysql/ 3/14
9/7/23, 1:39 AM Upload Multiple Images and Store in Database using PHP and MySQL - CodexWorld
menu Insert image file names in the database using PHP and MySQL.
https://www.codexworld.com/upload-multiple-images-store-in-database-php-mysql/ 4/14
9/7/23, 1:39 AM Upload Multiple Images and Store in Database using PHP and MySQL - CodexWorld
menu if(!empty($insertValuesSQL)){
$insertValuesSQL = trim($insertValuesSQL, ','); search
// Insert image file name into database
$insert = $db->query("INSERT INTO images (file_name, uploaded_o
n) VALUES $insertValuesSQL");
if($insert){
$statusMsg = "Files are uploaded successfully.".$errorMsg;
}else{
$statusMsg = "Sorry, there was an error uploading your file.";
}
}else{
$statusMsg = "Upload failed! ".$errorMsg;
}
}else{
$statusMsg = 'Please select a file to upload.';
}
}
?>
Include the database configuration file to connect and select the MySQL database.
Retrieve images from the server (uploads/) and listed on the web page.
<?php
// Include the database configuration file
include_once 'dbConfig.php';
// Get images from the database
$query = $db->query("SELECT * FROM images 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="" /> keyboard_double_arrow_up
<?php }
https://www.codexworld.com/upload-multiple-images-store-in-database-php-mysql/ 5/14
9/7/23, 1:39 AM Upload Multiple Images and Store in Database using PHP and MySQL - CodexWorld
menu }else{ ?>
<p>No image(s) found...</p> search
<?php } ?>
SEE ALSO: Create Dynamic Image Gallery with jQuery, PHP & MySQL
Conclusion
Here we have shown the easiest way to integrate multiple image upload functionality on the website.
This example code also is used to upload multiple files in PHP. You can extend these multiple image
upload functionality as per your needs. For user-friendly image upload, you can implement Drag and
drop file upload using Dropzone JS and PHP. Alternatively, the Ajax multiple images upload will provide
a better user-interface to the website.
menu search
keyboard_double_arrow_up
https://www.codexworld.com/upload-multiple-images-store-in-database-php-mysql/ 7/14
9/7/23, 1:39 AM Upload Multiple Images and Store in Database using PHP and MySQL - CodexWorld
menu search
15 COMMENTS
Rudy Said...
hI Thanks,
how to randomly rename the images ?
January 28, 2023 at 12:18 PM
Shoyo Said...
u saved my day, thank u (y)
keyboard_double_arrow_up
July 25, 2021 at 2:02 AM
Tayyab Said...
https://www.codexworld.com/upload-multiple-images-store-in-database-php-mysql/ 8/14
9/7/23, 1:39 AM Upload Multiple Images and Store in Database using PHP and MySQL - CodexWorld
Ev Said...
Hi! Thanks a lot for the code it helped me a lot and I’m just getting started with PHP. I’m
working on a hobby project and I’ve been trying to add something like a “batch id” that I
can manually input in the form upon uploading the images but I just can’t seem to get it
right, is there any other example I can look at?
Thanks a lot!
Ev.
November 24, 2019 at 9:24 PM
Meshael Said...
Kevin make user ID i.e Bob’s ID from the user table a foreign key in the image table by
adding another column. You can then get images by the user ID;
September 5, 2019 at 5:26 PM
Anshu Said...
how to fetch uploaded image in slider
August 28, 2019 at 8:47 AM
Murph Said...
Is the status column in the table necessary? I don’t see where it’s used in the rest of the
code, aside from defaulting to one if an entry is created.
August 11, 2019 at 8:03 PM
CodexWorld Said...
No, the status field is not necessary. But, it will be useful for the functionality
extension in the future.
August 12, 2019 at 4:50 PM
Kevin Said...
Your code just worked fine. Thank you. i have a question,
how if i upload 3 photo with 1 user ?
example : keyboard_double_arrow_up
https://www.codexworld.com/upload-multiple-images-store-in-database-php-mysql/ 9/14
9/7/23, 1:39 AM Upload Multiple Images and Store in Database using PHP and MySQL - CodexWorld
menuname | photo
Bob | beach.jpg, home.jpg, toilet.jpg search
sorry my bad english.
June 9, 2019 at 4:44 PM
Mintu Said...
Your code just worked fine. Thank you.
May 2, 2019 at 4:46 PM
LEAVE A REPLY
keyboard_double_arrow_up
https://www.codexworld.com/upload-multiple-images-store-in-database-php-mysql/ 10/14
9/7/23, 1:39 AM Upload Multiple Images and Store in Database using PHP and MySQL - CodexWorld
menuComment* search
Your Name*
Your Email*
Your Website
Post Comment
SUBSCRIBE
keyboard_double_arrow_up
https://www.codexworld.com/upload-multiple-images-store-in-database-php-mysql/ 11/14
9/7/23, 1:39 AM Upload Multiple Images and Store in Database using PHP and MySQL - CodexWorld
menu search
TRENDING TUTORIALS
PHP CodeIgniter
WordPress JavaScript
GoogleMap HTML&CSS
Bootstrap CakePHP
Laravel PayPal
keyboard_double_arrow_up
https://www.codexworld.com/upload-multiple-images-store-in-database-php-mysql/ 12/14
9/7/23, 1:39 AM Upload Multiple Images and Store in Database using PHP and MySQL - CodexWorld
menu search
CodexWorld is the most popular Programming & Web Development website aiming to provide the best
online resources for web application developers and designers.
ABOUT US CONTACT
keyboard_double_arrow_up
https://www.codexworld.com/upload-multiple-images-store-in-database-php-mysql/ 14/14