XMPP JavaScript Library

How to upload multiple files in PHP

PHP, Sachin Puri, 2012-02-27 17:28:03

This is an old script please see this new Article

This is just a demonstration for uploading multiple files. Please take care of security aspects like checking the extension, file type, size of file being uploded. 

Concept is very simple we will give user a html form with a single file element to upload one file and we will provide a link to add more file elements for uploading more files. 

Note: 

Create a folder "upload" to save uploaded files in it.

Javascript

function add_file_uploader()
{
     var container=document.getElementById('image_upload_container');
     var file_uploader=document.createElement('input');
     file_uploader.type='file';
     file_uploader.name='txtImage[]';
     var new_line=document.createElement('br');
     container.appendChild(new_line);
     container.appendChild(file_uploader);
}

HTML

<form name="frm1" method="post" style="margin-left:10px;" enctype="multipart/form-data">
     <div id="image_upload_container">
          <input type="file" name="txtImage[]" value="" size="40"/>
     </div>
     <a href="" onClick="add_file_uploader(); return false;">Add More</a><br/>
     <input type="submit" name="cmdSubmit" value="Upload" size="50"/>
</form>

PHP

if(isset($_POST['cmdSubmit']))
{
   $no_of_images=count($_FILES['txtImage']['name']); 
   for($i=0; $i<$no_of_images; $i++)
   {
      $image=$_FILES['txtImage']['name'][$i];
      $tmp_name=$_FILES['txtImage']['tmp_name'][$i];
      $image="upload/".time() . "_" . str_replace(" ","_",strtolower($image));
      move_uploaded_file($tmp_name,$image);
   }
}
 
 
 
 

 

Add Your Comment
   
    Yes! I want to receive all comments by email

  by vishal on 21-Jun-2012 03:14 pm
awesome one
  • Reply
  •  2 Like
  •  2 Dislike
  • Report