Home > Article > Backend Development > Upload multiple images in PHP
php Xiaobian Yuzai will introduce you how to upload multiple images in PHP. In website development, it is often necessary to implement the function of batch uploading images. In order to improve user experience and efficiency, uploading multiple images is a common requirement. PHP provides a wealth of functions and technologies to implement this function, including using forms, processing uploaded files, processing multiple files in a loop, etc. Through the guidance of this article, you will learn how to easily upload multiple images in PHP to add more interactive and creative elements to your website.
To make this possible, we need to specify the form action in our HTML file or section depending on how you structure your code base, and then use a built-in function to handle that action.
In this article, we will learn how to upload multiple images in PHP, which provides us with the ability to specify the required files from a form input, process all user-selected files, and upload or move to the desired location context.
$_FILES
When the user puts any input into the HTML form, we use the POST method to send any input (from text to file) to the server side where our PHP application lives.
<fORM method='post' action='' enctype='multipart/form-data'>
enctype='multipart/form-data'
part specifies the encoding method of the form data, which is required when we use file upload in the form.
For file upload we need to enter the type file and specify the name (can be any name you decide) for the file.
<input type="file" name="file" id="file">
For multiple file uploads, we still need the input type file, but now with a different specified name file[]
and the added attribute multiple. Adding []
indicates that the input field can handle multiple files.
<input type="file" name="files[]" multiple/>
On the server side, the global variable $_FILES
is an associative array that contains files uploaded via the Http POST method, allowing us to handle the files appropriately.
<?php $_FILES["files"]
Now that we understand the basics, we need to upload multiple files. Let's create a PHP form to upload multiple images.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Multiple Image Upload</title> </head> <body> <form method="post" enctype="multipart/form-data" name="formUploadFile"> <label>Select image(s) to upload:</label> <input type="file" name="files[]" multiple="multiple" /> <input type="submit" value="Upload File" name="imgSubmit" /> </form> <?php if (isset($_POST["imgSubmit"])) { $Upload multiple images in PHPs = []; $uploadedFiles = []; $extension = array("jpeg", "jpg", "png"); $UploadFolder = "images"; $counter = 0; foreach ($_FILES["files"]["tmp_name"] as $key => $tmp_name) { $temp = $_FILES["files"]["tmp_name"][$key]; $name = $_FILES["files"]["name"][$key]; if (empty($temp)) { break; } $counter++; $UploadOk = true; $ext = pathinfo($name, PATHINFO_EXTENSION); if (in_array($ext, $extension) == false) { $UploadOk = false; array_push($Upload multiple images in PHPs, $name . " isn't an image."); } if ($UploadOk == true) { move_uploaded_file($temp, $UploadFolder . "/" . $name); array_push($uploadedFiles, $name); } } if ($counter > 0) { if (count($Upload multiple images in PHPs) > 0) { echo "<b>Errors:</b>"; echo "<br/><ul>"; foreach ($Upload multiple images in PHPs as $Upload multiple images in PHP) { echo "<li>" . $Upload multiple images in PHP . "</li>"; } echo "</ul><br/>"; } if (count($uploadedFiles) > 0) { echo "<b>Uploaded Files:</b>"; echo "<br/><ul>"; foreach ($uploadedFiles as $fileName) { echo "<li>" . $fileName . "</li>"; } echo "</ul><br/>"; echo count($uploadedFiles) . " iamge(s) are successfully uploaded."; } } else { echo "Please, Select image(s) to upload."; } } ?> </body> </html>
Check whether the $_POST[]
variable is set using the isset()
function, initialize important variables, and set the extension required for file upload.
if (isset($_POST["imgSubmit"])) { $Upload multiple images in PHPs = []; $uploadedFiles = []; $extension = array("jpeg", "jpg", "png"); $UploadFolder = "images";
After that we loop through the multiple images that have been processed via the $_FILES[]
variable and then check the extension using pathinfo()
and if true we move the image to the specified Folder $UploadFolder Use the move_uploaded_file()
function and push the name of the image to the $uploadedFiles variable.
foreach ($_FILES["files"]["tmp_name"] as $key => $tmp_name) { $temp = $_FILES["files"]["tmp_name"][$key]; $name = $_FILES["files"]["name"][$key]; if (empty($temp)) { break; } $counter++; $UploadOk = true; $ext = pathinfo($name, PATHINFO_EXTENSION); if (in_array($ext, $extension) == false) { $UploadOk = false; array_push($Upload multiple images in PHPs, $name . " isn't an image."); } if ($UploadOk == true) { move_uploaded_file($temp, $UploadFolder . "/" . $name); array_push($uploadedFiles, $name); } }
Finally, we show the existing Upload multiple images in PHPs and the uploaded files.
if ($counter > 0) { if (count($Upload multiple images in PHPs) > 0) { echo "<b>Errors:</b>"; echo "<br/><ul>"; foreach ($Upload multiple images in PHPs as $Upload multiple images in PHP) { echo "<li>" . $Upload multiple images in PHP . "</li>"; } echo "</ul><br/>"; } if (count($uploadedFiles) > 0) { echo "<b>Uploaded Files:</b>"; echo "<br/><ul>"; foreach ($uploadedFiles as $fileName) { echo "<li>" . $fileName . "</li>"; } echo "</ul><br/>"; echo count($uploadedFiles) . " image(s) are successfully uploaded."; } } else { echo "Please, Select image(s) to upload."; }
PHP file served to the browser.
Select an image and upload the image.
Then, the uploaded file is displayed.
Uploaded image:
If the file you select is not an image, an Upload multiple images in PHP will appear.
The above is the detailed content of Upload multiple images in PHP. For more information, please follow other related articles on the PHP Chinese website!