uploading multiple files in php

To upload multiple files in php, we can use the following code. You need to set the path of upload directory $uploaddir in the code

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>

</head>

<body>
<?php

if( isset($_POST['btnsubmit']) )
{
//print_r($_FILES);
$uploaddir = 'A:/AAA/www/web/uploads/';

  foreach($_FILES["userfile"]["error"] as $key => $error)
  {

    if( $error == UPLOAD_ERR_OK)
    {
      $tmp_name = $_FILES["userfile"]["tmp_name"][$key];	
      $name = basename($_FILES["userfile"]["name"][$key]);
      if( move_uploaded_file($tmp_name, $uploaddir.$name) )
        echo "file ". $name . " uploaded <br>";
      else
        echo "file ". $name . " failed to upload <br>";
      
    }
  }
}	
  
  
?>
<form action="" method="post" enctype="multipart/form-data" name="form1">
  <input type="file" name="userfile[]" id="userfile[]"> <br>
  <input type="file" name="userfile[]" id="userfile[]"> <br>
  <input type="file" name="userfile[]" id="userfile[]"> <br>
  <input id="btnsubmit" name="btnsubmit" type="submit" value="Upload">
</form>
</body>
</html>

 

Add a Comment

Your email address will not be published. Required fields are marked *