Upload multiple files using PHP

http://www.tricing.com/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”.
<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

  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

Top