Monday 2 February 2015

How to upload multiple file at one time in php


<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11./jquery.min.js"></script>
<script>
$(document).ready(function(){
 $("#AddMore").click(function(){
var inpunt_length=$('.addnew >tbody >tr').length;  numR = parseInt(inpunt_length);
var newRow=$('<tr><td><input type="file" name="f[]"></td></tr>'); $('table.addnew> tbody > tr').eq(-1).after(newRow);
numR++; });   
});
</script>
</head>
<form action="" method="post" enctype="multipart/form-data">
<body>
<table class="lessons-list addnew" border="1"  id="CloneTable"> 
<tr><td colspan="5">&nbsp;</td></tr>
<tr><td width="25%" align="left" valign="middle" class="paddBot11"><input type="file" name="f[]"></td>
<td width="25%" align="left" style="font-weight:bold" class="LinkSpan"><a href="javascript:void(0);" id="AddMore" style="color:#090;font-weight:bold;"> + Add More</a></td>
</tr></table>
</body>
<input type="submit" name="upload" value="UPLOAD">
</form>
</html>
<?php
if(isset($_POST['upload']))
{
    $a=count($_FILES['f']['name']);
    foreach ($_FILES['f']['name'] as $filename)
    {
        for($i=0;$i<$a;$i++)
        {  
            $file=$_FILES['f']['name'][$i];
            $tmp=$_FILES['f']['tmp_name'][$i];
            $fs=$_FILES['f']['size'][$i];
            $ft=$_FILES['f']['type'][$i];
            $target="upload/".$file;                /* upload is my target directory name */
             if($_REQUEST['upload'])
               {
                    $result=move_uploaded_file($tmp,$target);
                     if($result==1)
                      {
                       $suceesful_file[]=$file;
                      }else
                     {
                       $unsuccesfile[] = $file; 
                      }  
               }       
          }
 }
              $count_file=  count($sucessful_file);
              echo "file uploaded sucessfully!! ".$count_file;
              $count_unsuccessfile=  count($unsuccesfile);
              if($count_unsuccessfile>0)
                {
                   echo "file not uploaded! ".$count_unsuccessfile;
                   foreach($unsuccesfile as  $value)
                   {
                       echo "Not uploaded !!".$value;
                   }
               }
}
?>