Search code examples
phphtmlfilefile-uploadmultifile-uploader

Uploading 3 (multiple) files with PHP - files are not getting saved in the target folder


Form to upload files:

   <form action="<?Php echo $_SERVER["PHP_SELF"];?>"method="post"enctype="multipart/form-data">
   <input type="file"name="uf[]">
  <input type="file"name="uf[]">
 <input type="file"name="uf[]">
 <input type="submit"value="upload"name="ok">
 </form>

PHP script to receive files:

  <?php
  if(!isset($_POST["ok"]))
   {echo "Sorry ,could not upload!";}
 else
 {     $f1=$_FILES["uf"]["name"][0];
   $f2=$_FILES["uf"]["name"][1];        $f3=$_FILES["uf"]["name"][2];
     $path="path/";$filea=$path.$f1;
   $fileb=$path.$f2;$filec=$path.$f3;
  move_uploaded_file($_FILES["uf"][0]["tmp_name"],$filea);
 move_uploaded_file($_FILES["uf"][1]["tmp_name"],$fileb);
 move_uploaded_file($_FILES["uf"][0]["tmp_name"],$filec);}
  ?>

The files are not getting saved and I am getting a user defined error


Solution

  • In your case if the form is not send you print `"Sorry ,could not upload!" your error was in this lines:

     move_uploaded_file($_FILES["uf"][0]["tmp_name"],$filea);
     move_uploaded_file($_FILES["uf"][1]["tmp_name"],$fileb);
     move_uploaded_file($_FILES["uf"][0]["tmp_name"],$filec);
    

    Try with this:

      <?php
      if(!isset($_POST["ok"]))
       {echo "Sorry ,could not upload!";}
     else
     { 
       $f1=$_FILES["uf"]["name"][0];
       $f2=$_FILES["uf"]["name"][1];        
       $f3=$_FILES["uf"]["name"][2];
    
       $path="path/";
       $filea=$path.$f1;
       $fileb=$path.$f2;
       $filec=$path.$f3;
    
      move_uploaded_file($_FILES["uf"]["tmp_name"][0],$filea);
      move_uploaded_file($_FILES["uf"]["tmp_name"][1],$fileb);
      move_uploaded_file($_FILES["uf"]["tmp_name"][2],$filec);
    
    }
    
    
      ?>