
In my previous post, upload a single files, rename a file, file size validation, file type validation. In this, How to upload multiple files using PHP. Link for previous posts as follow.
Click here – Upload a single file using PHP
Click here – Upload a single file using PHP – II (rename, size and type validation)
Step1: Create the following elements using the html
- Form with enctype “multipart/form-data”, method “post”.
- Input type=”file”, name=”attachment[]” and mutiple=”multiple” (allows multiple values).
- Input type=”submit”.
<html> <head><title>Multiple File Upload</title></head> <body> <form method="post" enctype="multipart/form-data" > <input type="file" name="attachment[]" multiple="multiple" /> <input type="submit" name="Submit" value="Upload" /> </form> </body> </html>
Step2: The following PHP script used to upload multiple files.
<?PHP if($_POST['Submit']=='Upload') { $file_count = sizeof($_FILES['attachment']['name']); $uploaded_count=0; for($i=0;$i<$file_count;$i++) { $attachment= $_FILES['attachment']['name'][$i]; if($attachment) { $path= "upload/".addslashes($attachment); if(move_uploaded_file($_FILES['attachment']['tmp_name'][$i], $path)) $uploaded_count++; } } echo $uploaded_count.' Files Successfully Uploaded...'; } ?>
In the above php script
- Get number file to be upload.
- Using for loop get the individual file name.
- Check the file exists.
- Finally upload the file to server.