Lets learn
1.Create file as imageuploading.php and implement code in it for uploading multiple images using POST method.
<form>
and add enctype='multiple/form-data'
attribute.
<form action="upload.php" method="POST" enctype="multipart/form-data">
type='file'
element and for enabling multiple files selection add multiple
attribute. For reading all selected files when <form>
submitted add [] brackets at the end of a name which denotes an Array.
<input type="file" name="file[]" id="file" multiple>
Complete Code to post multiple images in html:
<!DOCTYPE html>
<html lang="en">
<head>
<title>uploading multiple images in PHP using a for loop predicate</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container " style="margin-top: 70px">
<div class="text-center">
<h1>uploading multiple images in PHP using a for loop predicate</h1>
</div>
<br>
<br>
<h4></h4>
<div class="well">
<form action="upload.php" method="POST" enctype="multipart/form-data">
<p>
<input type="file" name="files[]" multiple>
<br><br>
<input type="submit" class="btn btn-success" name="submit" value="Upload" >
</p>
</form>
<br>
</div>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<title>uploading multiple images in PHP using a for loop predicate</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container " style="margin-top: 70px">
<div class="text-center">
<h1>uploading multiple images in PHP using a for loop predicate</h1>
</div>
<br>
<br>
<h4></h4>
<div class="well">
<?php
if (isset($_POST['submit'])) {
$countfiles = count($_FILES['files']['name']);
// Looping all files
// Configure upload directory and allowed file types
$upload_dir = 'upload' . DIRECTORY_SEPARATOR;
$allowed_types = array('jpg', 'png', 'jpeg', 'gif');
// Define maxsize for files i.e 2MB
$maxsize = 2 * 1024 * 1024;
// Checks if user sent an empty form
if (!empty(array_filter($_FILES['files']['name']))) {
for ($i = 0; $i < $countfiles; $i++) {
echo $i ."/".$countfiles ;
$filename = $_FILES['files']['name'][$i];
$file_tmpname = $_FILES['files']['tmp_name'][$i];
$file_name = $_FILES['files']['name'][$i];
$file_size = $_FILES['files']['size'][$i];
// Upload file
$file_ext = pathinfo($file_name, PATHINFO_EXTENSION);
$filepath = $upload_dir . $file_name;
if (in_array(strtolower($file_ext), $allowed_types)) {
// Verify file size - 2MB max
if ($file_size > $maxsize)
echo "Error: File size is larger than limit.";
// If file with name already exist then append time in
// front of name of the file to avoid overwriting of file
if (file_exists($filepath)) {
$filepath = $upload_dir . $file_name;
if (move_uploaded_file($file_tmpname, $filepath)) {
echo " {$file_name} successfully uploaded <br />";
} else {
echo "Error uploading {$file_name} <br />";
}
} else {
if (move_uploaded_file($file_tmpname, $filepath)) {
echo " {$file_name} successfully uploaded <br />";
} else {
echo "Error uploading {$file_name} <br />";
}
}
} else {
// If file extention not valid
echo "Error uploading {$file_name} ";
echo "({$file_ext} file type is not allowed)<br / >";
}
}
} else {
// If no files selected
echo "No files selected.";
}
}
?>
<br>
</div>
</div>
</body>
</html>
3.Explanation of Code.
if any user uploaded a file using this field, we can obtains its details like the name, type, size, temporary name or any error occurred while attempting the upload via the $_FILES["files"]
associative array, like this:
$_FILES["files"]["name"]
— This array value specifies the original name of the file, including the file extension. It doesn't include the file path.
$_FILES["files"]["type"]
— This array value specifies the MIME type of the file.
$_FILES["files"]["size"]
— This array value specifies the file size, in bytes.
$_FILES["files"]["tmp_name"]
— This array value specifies the temporary name including full path that is assigned to the file once it has been uploaded to the server.
$_FILES["files"]["error"]
— This array value specifies error or status code associated with the file upload, e.g. it will be 0, if there is no error.