Upload Multiple Images and Store in Database Using PHP 8 And MySQL
Jun 02, 2022 . Admin
Hello Friends,
Now, let's see article of Upload Multiple Images And Store In Database Using PHP 8 And MySQL. I explained simply about PHP 8 MySQL Multiple Image Upload Example. This article goes in detailed on How to Upload Multiple Images in PHP 8 with MySql Example. This article goes in detailed on Upload Multiple Images using PHP 8 & MySQL. Let's see bellow example Multiple image upload concept using php & mysql.
We will use get simple example of Multiple image upload concept using php 8 & mysql
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>Upload Multiple Images And Store In Database Using PHP 8 And MySQL</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>Upload Multiple Images And Store In Database Using PHP 8 And MySQL - 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...