HTML Form
<?php
ob_start();
session_start();
require_once('../1config/conn.php');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Upload</title>
<?php
require_once('../1config/all_css.php');
?>
</head>
<body>
<div class="container mt-5 p-0">
<div class="row">
<div class="col-lg-12">
<div class="card">
<div class="card-header"><h4>Multiple image upload in php</h4></div>
<div class="card-body">
<form action="../3management/upload.php" method="post" enctype="multipart/form-data" >
<div class="row">
<div class="col-lg-12">
<?php
if(isset($_SESSION['success']) && ($_SESSION['success'] != ''))
{
echo "<div class='alert alert-success'>".$_SESSION['success']."</div>";
session_unset();
}
if(isset($_SESSION['error']) && ($_SESSION['error'] != ''))
{
echo "<div class='alert alert-danger'>".$_SESSION['error']."</div>";
session_unset();
}
?>
</div>
</div>
<div class="row m-2">
<div class="col-lg-6">
<input type="file" name="images[]" id="selectimage" class="form-control" autocomplete="off" placeholder="Enter Your Name" multiple>
<button type="submit" class="mt-3 submitbutton btn btn-primary" name="uploadbutton">SUBMIT</button>
</div>
<div class="col-lg-6">
<img src="../4assets/product-placeholder.jpg" alt="" width="300px" height="300px" id="imageobject">
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<?php
require_once('../1config/all_js.php');
?>
</body>
</html>
- It includes meta tags for the character set and viewport settings.
- It sets the title of the page to "Image Upload".
- It includes CSS files using require_once('../1config/all_css.php').
- A container <div> with Bootstrap classes is used for layout.
- A form is created with the action set to "../3management/upload.php". This form is used to upload images.
- The enctype attribute is set to "multipart/form-data" to allow file uploads.
- There are two file input fields with the name "images[]", allowing multiple file uploads.
- A submit button with the name "uploadbutton" is included.
- It checks if there is a success or error message stored in the session variables ($_SESSION['success'] and $_SESSION['error']).
- If there is a success message, it displays it in a green alert box.
- If there is an error message, it displays it in a red alert box.
- After displaying the messages, it unsets the session variables to clear them.
Connection Code
PHP code for file upload
<?php
ob_start();
session_start();
require_once('../1config/conn.php');
if(isset($_POST['uploadbutton'])) {
$imageNames = $_FILES['images']['name'];
$imageTempNames = $_FILES['images']['tmp_name'];
$imageRenameArr = array();
$imageSizeArr = array();
$imageExtensionArr = array();
$imagePathArr = array();
foreach ($imageNames as $key => $imageName)
{
$imageExtension = pathinfo($imageName, PATHINFO_EXTENSION);
$imagerename = 'image_' . time() . '_' . $key . '.' . $imageExtension;
$imageSaveLocation = '../4assets/images/'.$imagerename;
$imageSizeBytes = filesize($imageTempNames[$key]);
$imagesizeMB = round(($imageSizeBytes / 1024) / 1024, 2);
$allowed_extensions = array("jpeg", "jpg", "png");
if($imagesizeMB > 5) {
$_SESSION['error'] = 'File size must be less than 5MB.';
header('location:../2view/index.php');
exit();
} else if(!in_array($imageExtension, $allowed_extensions)) {
$_SESSION['error'] = 'Extension not allowed, please choose a JPEG, JPG, or PNG file.';
header('location:../2view/index.php');
exit();
} else {
if(move_uploaded_file($imageTempNames[$key], $imageSaveLocation)) {
$imageRenameArr[] = $imagerename;
$imageSizeArr[] = $imagesizeMB;
$imageExtensionArr[] = $imageExtension;
$imagePathArr[] = $imageSaveLocation;
$_SESSION['success'] = 'Image uploaded successfully.';
} else {
$_SESSION['error'] = 'Error uploading image.';
header('location:../2view/index.php');
exit();
}
}
}
$imageNameString = implode(",", $imageRenameArr);
$imageSizeString = implode(",", $imageSizeArr);
$imageExtensionString = implode(",", $imageExtensionArr);
$imagePathString = implode(",", $imagePathArr);
$insert_record = mysqli_query($conn, "INSERT INTO `multiple_images` (`image_name`,`size`, `extension`,`path`) VALUES ('$imageNameString','$imageSizeString','$imageExtensionString','$imagePathString')");
if($insert_record)
{
$_SESSION['success'] = 'Image uploaded successfully.';
header('location:../2view/index.php');
}
else
{
$_SESSION['error'] = 'Error uploading image to database.';
header('location:../2view/index.php');
}
}
?>