0

我想上传多个文件而不是一个。我知道如何上传单个文件(图片) 我需要帮助上传多个文件。您可以在下面看到我如何上传单个文件。

这是我添加时的HTML:

    <form action="upload_file.php" method="post" enctype="multipart/form-data">
    <label for="file">Filename:</label>
    <input type="file" name="file" id="file" multiple><br>
    <input type="submit" name="submit" value="Submit">
    </form>

我看到有些人将name="file"更改为name="file[]"。但我不知道如何用php上传它。

这是PHP代码

   <?php
    $allowedExts = array("gif", "jpeg", "jpg", "png");
    $temp = explode(".", $_FILES["file"]["name"]);
    $extension = end($temp);

    $random_digit=rand(0000,9999);

    if ((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/jpg")
    || ($_FILES["file"]["type"] == "image/pjpeg")
    || ($_FILES["file"]["type"] == "image/x-png")
    || ($_FILES["file"]["type"] == "image/png"))
    && ($_FILES["file"]["size"] < 20000)
    && in_array($extension, $allowedExts))
    {
    if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
   }
   else
   {

  if (file_exists("upload/" . $_FILES["file"]["name"]))
  {
      echo $_FILES["file"]["name"] . " already exists. ";
  }
  else
  {
  $file_name = $_FILES["file"]["name"];
  $ext = pathinfo($file_name, PATHINFO_EXTENSION); // Get the extension of a file
  $new_file_name = $random_digit . '.' . $ext;
  move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $new_file_name);
  echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
     }
   }
 }
else
 {
 echo "Invalid file";
 }
 ?> 
4

2 回答 2

3

如果您使用name="file[]" $_FILES["file"]["name"]将是一个 array() 您可以使用它进行测试,print_r($_FILES["file"])或者var_dump($_FILES["file"])您的数组将如下所示:

Array
(
[uploads] => Array
    (
        [name] => Array
            (
                [0] => hack-attempt.sh
                [1] => picture.gif
                [2] => text.txt
                [3] => virus.exe
            )

        [type] => Array
            (
                [0] => application/sh
                [1] => image/jpeg
                [2] => text/plain
                [3] => applicatio/octet-stream
            )

        [tmp_name] => Array
            (
                [0] => /tmp/hack-attempt.sh
                [1] => /tmp/picture.gif
                [2] => /tmp/text.txt
                [3] => /tmp/virus.exe
            )

        [error] => Array
            (
                [0] => 0
                [1] => 0
                [2] => 0
                [3] => 0
            )

        [size] => Array
            (
                [0] => 217297
                [1] => 53600
                [2] => 6511924
                [3] => 127662
            )

    )

)

示例取自:http ://staticfloat.com/php-programmieren/multiupload-mit-html5-und-php/它是德语但只是为了信用。

于 2013-10-08T20:14:27.093 回答
1

您的上传表单需要有多个同名的输入字段。

让我们称这些为“上传 []”

然后在您的接收脚本中,您需要遍历 $_FILES[] 数组以获取每个数组。

$response = "";
$i = 0;
while ((isset($_FILES['upload']['name'][$i])) && ($_FILES['upload']['name'][$i]<>"")){
    $error = $_FILES['upload']['error'][$i];

    if ($error == 0){

        $upfilename = $_FILES['upload']['name'][$i];
        //should probably validate your filename here
            //$upfilename = validate_fname($upfilename)

        $tmpname = $_FILES['upload']['tmp_name'][$i];
        $dirname = "uploads" // your files to be stored in this directory
        if (move_uploaded_file($tmpname, $dirname."/".$filename)){
            $response .= "<br><div class=\"message\">File : $filename : uploaded successfully. Thank you.</div><br>";
        }else {
            $response .= "<br> <div class=\"message\"> There was an error uploading file: $filename ";
        }

    }
    else $response = "<br><div class=\"errormsg\">there was an error - error code: $error</div><br>";
    $i++;
}
于 2013-10-08T20:17:08.090 回答