How to upload multiple files using PHP and HTML
upload multiple files using html and php, PHP code to upload multiple files, HTML code to upload multiple file

Using a single input file element, you can upload multiple files in PHP. You only need to modify the PHP code for uploading a single file and enable the file element to select multiple files.
We'll look at using HTML and PHP to upload multiple files in this article.The user can select multiple files at once and upload all of them to the server using the multiple image upload feature.
index.html is a file of HTML page that allows users to select multiple files and upload them to the server. A form to select and upload files using the POST method can be found in this HTML file. Which is sending to the server where PHP will handle the rest
Select and upload multiple
files to the server
Upload Files
Select files to upload:
in the above code, in section we are giving name as files[ ] where all the selected files will be stored as array. We must add one attribute name multiple in the input tag.
Now to handle the above code in the server, we will make a file with the name file_upload.php in PHP to upload these multiple files to the server.
below is the code of file_upload.php.
";
}
else {
echo "Error uploading {$file_name}
";
}
}
else {
if( move_uploaded_file($file_tmpname, $filepath)) {
echo "{$file_name} successfully uploaded
";
}
else {
echo "Error uploading {$file_name}
";
}
}
}
else {
// If file extension not valid
echo "Error uploading {$file_name} ";
echo "({$file_ext} file type is not allowed)
";
}
}
}
else {
// If no files selected
echo "No files selected.";
}
}
?>
$upload_dir
is used to mention the directory where the image will be saved.
$allowed_types
: it is an array where we can say what kind of file will be supported to store in the server.
$maxsize
: defining the maximum size of the file.
As files are stored in the $_FILES variable. Now we can check whether files are sent from the client side or not using HTML post request with array_filter
function of array in php.
Iterating all the files one by one using the foreach
loop. We will gather information about the files sent. Information such as file name, size, and extensions. We will only allow files that we want to, using
if
(in_array(
strtolower
(
$file_ext
),
$allowed_types
))
this will help filter out all unnecessary files other than the mentioned extensions.
After everything is done we will upload the files in the server using the PHP function move_uploaded_file
.
What's Your Reaction?






