在创建目录并使用单个浏览按钮上传多个文件时

时间:2016-03-28 19:42:37

标签: php html

我正在尝试使用单个浏览按钮上传多个文件,但我无法通过它获得成功。我试过类似查询中提到的建议。下面的代码只上传一个文件,我需要使用相同的浏览按钮上传多个文件。

知道代码中有什么问题吗?

<html>
<body>

<form enctype="multipart/form-data" action="uploadj.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile[]" type="file" multiple /><br />



  <p>

    <input type="submit" name="button" id="button" value="Submit">
  </p>

</form>


</html>
</body>

Php Code :


   <?php

$get_folder = $_POST['textfield'];
mkdir ("/opt/lampp/htdocs/test_upload/" . $get_folder, 0777);
echo "Analysis Directory created successfully";

$target_path = "$get_folder/";


for($i=0;$i < count($_FILES['uploadedfile']['name']);$i++){



$target_path = $target_path . basename( $_FILES['uploadedfile']['name'][$i]);


if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'][$i], $target_path))
{
    echo "The file has been uploaded";

} else

{
  echo "There was an error uploading the file, please try again!";
}


}



?>

谢谢!

3 个答案:

答案 0 :(得分:1)

这是错误的:$ _FILES [&#39;上传文件&#39;] [0] [&#39;名称&#39;]

你应该这样做:$ _FILES [&#39;上传文件&#39;] [&#39;名称&#39;] [0]

我已更新此代码以支持多次上传。如果有效,请告诉我

     for($i=0;$i<count($_FILES['uploadedfile']['tmp_name']);$i++){
      $target_path = ""; //to clear the values on each loop//
      $target_path = basename( $_FILES['uploadedfile']['name'][$i]);
      move_uploaded_file($_FILES['uploadedfile']['tmp_name'][$i], $target_path);
     }

答案 1 :(得分:0)

您遇到的一个错误是在定义之前使用$target_path。我指的是:

$target_path = $target_path . basename( $_FILES['uploadedfile'][0]['name']);

如果之前未定义,则不能在右侧使用$target_path。最重要的是,0处于错误的位置。它应该是$_FILES['uploadedfile']['name'][0]

您可能遇到的其他错误:

  1. php.ini
  2. 中的文件上传大小不够高
  3. php.ini
  4. 中的最大输入大小不够高
  5. 网络服务器用户(通常是www-data或apache)无法写入文件夹
  6. <强>更新

    另一个错误,就是你只是在你的循环中附加$target_path这使得第二次迭代试图将之前的上传用作文件夹,这是不允许的。例如,如果我上传了foo.png和bar.jpg,则第二个目标路径将是/opt/lampp/htdocs/test_upload/foo.png/bar.png,这不是有效路径。

    我会通过改变这一行在你的循环中解决这个问题:

    $target_path = $target_path . basename( $_FILES['uploadedfile']['name'][$i]);
    

    为:

     $target_file =  $target_path . basename( $_FILES['uploadedfile']['name'][$i]);
    

    并改变:

    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'][$i], $target_path))
    

    为:

    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'][$i], $target_file))
    

    更新2

    以下代码适用于我的服务器(Ubuntu Server上传文件夹属于www-data,权限为755):

    <?php if($_SERVER['REQUEST_METHOD'] != 'POST'):?>
    <html>
    <body>
    
    <form enctype="multipart/form-data" action="index2.php" method="POST">
    Choose a file to upload: <input name="uploadedfile[]" type="file" multiple /><br />
    
    
    
      <p>
    
        <input type="submit" name="button" id="button" value="Submit">
      </p>
    
    </form>
    
    
    </html>
    </body>
    
    <?php else: ?>
    
    <?php
    
    $target_path = "./videos/";
    
    
    for($i=0;$i < count($_FILES['uploadedfile']['name']);$i++){
    
    
    
    $target_file = $target_path . basename( $_FILES['uploadedfile']['name'][$i]);
    
    
    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'][$i], $target_file))
    {
        echo "The file has been uploaded";
    
    } else
    
    {
      echo "There was an error uploading the file, please try again!";
    }
    
    
    }
    ?>
    
    <?php endif; ?>
    

答案 2 :(得分:0)

FORM:

<form enctype="multipart/form-data" action="" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile[]" type="file" multiple /><br />

 <p>
 <input type="submit" name="button" id="button" value="Submit">
 </p>

</form>

PHP代码:

 if(isset($_POST['button'])){
     $upload_folder = "./textfiles/";
     for($i=0;$i<count($_FILES['uploadedfile']['tmp_name']);$i++){
          $target_path = ""; //to clear the values on each loop//
          $target_path = $upload_folder.basename( $_FILES['uploadedfile']['name'][$i]);
          if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'][$i], $target_path))
             {
             //do your redirect
             }else {
                //do your redirect
             }
   }
  }

注意:php代码将放在表单的顶部!我已经测试了这段代码,它在我的结尾很有用: