03-Apr-2023
.
Admin
Hello Friends,
Today, I would like to show you how to upload multiple images in php 8 mysql?. We will look at example of Upload Multiple Images And Store In Database Using PHP 8 And MySQL. this example will help you Upload Multiple Images using PHP 8 & MySQL. I explained simply step by step Multiple image upload concept using php & mysql. you will do the following things for Multiple image upload concept using php 8 & mysql.
We will use get simple example of PHP 8 MySQL Multiple Image Upload Example
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>how to upload multiple images in php 8 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>how to upload multiple images in php 8 mysql? - Nicesnippets.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...
#PHP 8