In this article, we will look at how to upload Multiple Images And Store them In a Database Using PHP And MySQL. First, let's create an HTML form to upload multiple images or files to store the server and display them on the website. This Website form should contain the attributes as method=’post’ and enctype=’multipart/form-data to support file upload.
Next, Project structure your folder as follows.
├── css
├── Uploads
├── dbConfig.php
├── index.php
└── upload.php
MySQL database
Create a database called codeat21 and create a table called images with fields:
- Database Name – codeat21
- Table Name – images
codeat21CREATE TABLE `images` ( `id` int(11) NOT NULL AUTO_INCREMENT, `file_name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL, `uploaded_on` datetime NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
dbConfig.php<?php // Database configuration $dbHost = "localhost"; $dbUsername = "root"; $dbPassword = ""; $dbName = "codeat21"; // Create database connection $db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName); // Check connection if ($db->connect_error) { die("Connection failed: " . $db->connect_error); } ?>
Create a form with image upload in php.
The following php code will create a simple HTML form with a post method and save it an index.php file name. This code with a “choose file” option and an upload-to button.
index.php<!--File uploading script --> <?php include 'upload.php'; ?> <!DOCTYPE html> <html lang="en-US"> <head> <title>Upload Multiple Images And Store In Database Using PHP And MySQL by CodeAT21</title> <meta charset="utf-8"> <link rel="stylesheet" href="css/style.css"> </head> <body> <div class="App"> <h1> Upload Multiple Images And Store In Database Using PHP And MySQL </h1> <div class="wrapper"> <div class="form__field"> <?php if(!empty($statusMsg)){ ?> <p class="status__msg"><?php echo $statusMsg; ?></p> <?php } ?> <form action="" method="post" enctype="multipart/form-data"> <input type="file" name="images[]" class="form__field__img" multiple> <input type="submit" name="submit" value="Upload" class="btn__default"> </form> </div> <!-- gallery view of uploaded images --> <div class="gallery"> <h2>Uploaded Images</h2> <?php include 'dbConfig.php'; $query = $db->query("SELECT * FROM images ORDER BY uploaded_on DESC"); if($query->num_rows > 0){ while($row = $query->fetch_assoc()){ $imageURL = $row["file_name"]; ?> <img src="<?php echo $imageURL; ?>" alt="" /> <?php } }else{ ?> <p>No image(s) found...</p> <?php } ?> </div> </div> </body> </html>
- Use “upload.php” – file upload on the server
- Use method=”post” – sending the file to the server
- Use enctype=”multipart/form-data” – content-type
- Use type=”file” – on <input> tag
- Use move_uploaded_file – upload the file to folder
upload.php<?php include 'dbConfig.php'; $statusMsg = ''; if(isset($_POST['submit'])){ // File upload configuration $targetDir = "uploads/"; $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]; // File upload path $fileName = basename($_FILES['images']['name'][$key]); $targetFilePath = $targetDir . $fileName; // Check whether file type is valid $fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION); if(in_array($fileType, $allowTypes)){ // Store images on the server if(move_uploaded_file($_FILES['images']['tmp_name'][$key],$targetFilePath)){ $images_arr[] = $targetFilePath; $insert = $db->query("INSERT into images (file_name, uploaded_on) VALUES ('".$targetFilePath."', NOW())"); if($insert){ $count = $key + 1; $statusMsg = " ".$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 = 'Sorry, only JPG, JPEG, PNG, & GIF files are allowed to upload.'; } } } ?>
Related keywords
- How to upload multiple image in php and store in folder and database
- How to insert multiple image in mysql database using php source code
- multiple Image upload in PHP code with databases
- Upload multiple image in php mysql database and display
- PHP upload multiple image and preview display instantly
- PHP code for multiple image upload and display
- How to display multiple image in php
- How to display multiple image in php from database
- Upload multiple Image to Database and Server using HTML,PHP
- PHP multiple image upload
- PHP multiple image upload and preview
- multiple Image store in mysql database
- This website show country are united states, France, Canada, japan, New york, london, Los Angeles, Russia, Italy, Spain, Austria, Serbia, Moldova, Bosnia and Herzegovina, Albania, Lithuania, North Macedonia, Switzerland, Bulgaria, Denmark,Germany, United Kingdom.