Uploading Files and Navigating Directories in PHP - Multiple file uploads (
Page 3 of 4 )
Uploading multiple files simultaneously is not very different from uploading a single file. All you need to do is use a different name for each file that you want to use:
<form enctype="multipart/form-data" action="do_upload.php"
method="post"> Upload files:
<input name="Afile[]" type="file" size="80">
<input name="Afile[]" type="file" size="80">
<input name="Afile[]" type="file" size="80">
<br>
<input type="submit" value="Upload File">
</form>
When the above form is submitted, the arrays $_FILES['Afile'], $_FILES['A']['name'], and $_FILES['Afile']['size'] will be initialized and all of them will be numerically indexed. For example say you submit three files with the names uploader.html, fileprocess.php and what.php. The $_FILES['Afile']['name'][0] array will contain uploader.html and the $_FILES['Afile']['name'][1] array will contain what.php, and so on. All of the file attributes such as size and name will be available for each file that you submitted.
Once you've submitted the form you simply do a "for each" loop and display the form information.
Another way in which you can upload multiple files is by explicitly giving the "name" part of the form input a different name:
<form enctype="multipart/form-data" action="do_upload.php"
method="post"> Upload files:
<input name="Afile1" type="file" size="80">
<input name="Afile2" type="file" size="80">
<input name="Afile3" type="file" size="80">
<br>
<input type="submit" value="Upload File">
</form>
And then process them as normal.
Create another HTML document and call it uploaderfrm.html. Add the following code:
Uploader.html
<html>
<head>
<title>HTML Form for Multiple File Uploading</title>
</head>
<body>
<p>Multiple File Upload</p>
<br />
<form action="uploader_do.php" method="post"
enctype="multipart/form-data">
<p>Files:<br>
<input type="file" name="AFile[]" /><br>
<input type="file" name="AFile[]" /><br>
<input type="file" name="AFile[]" /><br>
<input type="submit" value="Upload Files" />
</p>
</form>
</body>
</html>
Now, let's create the script that will handle form data. Create a new PHP Document and save it as uploader_do.php.
Script:uploader_do.php
<?php
$uploaddir="c:uploads";
if(isset($_FILES['AFile']['error'])){
foreach ($_FILES["AFile"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["AFile"]["tmp_name"][$key];
$name = $_FILES["AFile"]["name"][$key];
move_uploaded_file($tmp_name, $uploaddir. "/" .$name);
}
}
}
?>
As with our previous upload script, first we define where we want to store the uploaded files by declaring '$uploaddir="c:uploads";' From this declaration we can see that our files are going to be stored on our C drive. Next we check whether there are any errors in the submitted File array:
'if(isset($_FILES['AFile']['error'])){ '
If there are any errors, the files will not be uploaded. If on the other hand there are no errors, the files are uploaded by the 'move_upload_file()' function.
There's another way that you can dynamically create input fields on a form to upload files. If you present a user with a form asking how many files he or she wants to upload, once you have the number you can simply create a dynamic form with n number of input tags and continue from there.