Upload multiple files using PHP


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

  1. Form with enctype “multipart/form-data”, method “post”.
  2. Input  type=”file”, name=”attachment[]” and mutiple=”multiple” (allows  multiple values).
  3. Input  type=”submit”.
<head><title>Multiple File Upload</title></head>
<form method="post" enctype="multipart/form-data" >
<input type="file" name="attachment[]" multiple="multiple"  />
<input type="submit" name="Submit" value="Upload" />

Step2: The following PHP script used to upload multiple files.

$file_count = sizeof($_FILES['attachment']['name']);  
$attachment= $_FILES['attachment']['name'][$i];
 $path= "upload/".addslashes($attachment); 
 if(move_uploaded_file($_FILES['attachment']['tmp_name'][$i], $path)) 
echo $uploaded_count.' Files Successfully Uploaded...'; 

In the above php script

  1. Get number file to be upload.
  2. Using for loop get the individual file name.
  3. Check the file exists.
  4. Finally upload the file to server.

Leave a Reply