【问题标题】:$_FILES array only stores one file PHP [duplicate]$_FILES 数组只存储一个文件 PHP [重复]
【发布时间】:2017-11-14 03:37:59
【问题描述】:

我试图让我的用户一次上传多个文件。我已经设置了我的表格以允许我相信的倍数;这是代码,所以有人可以帮助我确保它是正确的:

<form class="well" action="" method="post" enctype="multipart/form-data">
    <div class="form-group">
        <label for="file">Add Documents Such As (Certifications, Degrees, Prior Job Offers, Etc) Here:</label><br>
        <label for="file">Select a file to upload</label>
        <input type="file" name="filesToUpload" id="filesToUpload" multiple>
    </div>
    <button class="glyphicon glyphicon-open-file" type="submit" name="submit"/>Submit
</form>

这是我试图用来将所选文件上传到我的服务器的代码:

if (isset($_POST['submit'])) {
    $file = $_FILES['filesToUpload'];
    foreach ($_FILES['filesToUpload'] as $file) {
        $fileName = $file['name'];
        echo $fileName;
        $fileTmpName = $file['tmp_name'];
        $fileSize = $file['size'];
        $fileError = $file['error'];
        $fileType = $file['type'];

        $fileExt = explode('.', $fileName);
        $fileActualExt = strtolower(end($fileExt));

        $allowed = array('pdf', 'docx', 'doc', 'jpg', 'jpeg', 'txt', 'png');
        if (in_array($fileActualExt, $allowed)) {
            if ($fileError === 0) {
                if ($fileSize < 1000000) {
                    $fileNameNew = uniqid('', true).".".$fileActualExt;
                    $fileDestination = 'uploads/'.$fileNameNew;
                    move_uploaded_file($fileTmpName, $fileDestination);
                } else {
                    echo "Your file is too big!";
                  }
            } else {
                echo "There was an error uploading your file!";
              }
        } else {
            echo "You cannot upload files of this type!";
          }
    }
    header("Location: developer");
}

我注意到,当我在选择多个文件后回显 $file 时,它​​只显示其中一个文件的名称。我不确定,但我感觉这就是为什么我在尝试上传多个文件时只能将一个文件上传到服务器的原因。

有人可以确认这是否是问题所在吗?如果是的话,最好的方法是什么?

编辑:我已经尝试了围绕该网站提出的所有其他建议。当我使用&lt;input type="file" name="filesToUpload[]" id="filesToUpload" multiple&gt; 时,它会破坏它。我得到所有这些关于未定义索引的通知,我猜你在尝试分解文件的不同部分时称它们为扩展(基于下面有人所说的)。我还收到有关数组到字符串转换的通知。在输入中使用方括号也只是跳转到关于无法上传特定类型文件的 else 语句。

他们在这里:

Notice: Array to string conversion in /var/www/html/dev/my-documents/upload-document.php on line 47
Array
Notice: Undefined index: name in /var/www/html/dev/my-documents/upload-document.php on line 49

Notice: Undefined index: tmp_name in /var/www/html/dev/my-documents/upload-document.php on line 51

Notice: Undefined index: size in /var/www/html/dev/my-documents/upload-document.php on line 52

Notice: Undefined index: error in /var/www/html/dev/my-documents/upload-document.php on line 53

Notice: Undefined index: type in /var/www/html/dev/my-documents/upload-document.php on line 54
You cannot upload files of this type!
Notice: Undefined index: name in /var/www/html/dev/my-documents/upload-document.php on line 49

Notice: Undefined index: tmp_name in /var/www/html/dev/my-documents/upload-document.php on line 51

Notice: Undefined index: size in /var/www/html/dev/my-documents/upload-document.php on line 52

Notice: Undefined index: error in /var/www/html/dev/my-documents/upload-document.php on line 53

Notice: Undefined index: type in /var/www/html/dev/my-documents/upload-document.php on line 54
You cannot upload files of this type!
Notice: Undefined index: name in /var/www/html/dev/my-documents/upload-document.php on line 49

Notice: Undefined index: tmp_name in /var/www/html/dev/my-documents/upload-document.php on line 51

Notice: Undefined index: size in /var/www/html/dev/my-documents/upload-document.php on line 52

Notice: Undefined index: error in /var/www/html/dev/my-documents/upload-document.php on line 53

Notice: Undefined index: type in /var/www/html/dev/my-documents/upload-document.php on line 54
You cannot upload files of this type!
Notice: Undefined index: name in /var/www/html/dev/my-documents/upload-document.php on line 49

Notice: Undefined index: tmp_name in /var/www/html/dev/my-documents/upload-document.php on line 51

Notice: Undefined index: size in /var/www/html/dev/my-documents/upload-document.php on line 52

Notice: Undefined index: error in /var/www/html/dev/my-documents/upload-document.php on line 53

Notice: Undefined index: type in /var/www/html/dev/my-documents/upload-document.php on line 54
You cannot upload files of this type!
Notice: Undefined index: name in /var/www/html/dev/my-documents/upload-document.php on line 49

Notice: Undefined index: tmp_name in /var/www/html/dev/my-documents/upload-document.php on line 51

Notice: Undefined index: size in /var/www/html/dev/my-documents/upload-document.php on line 52

Notice: Undefined index: error in /var/www/html/dev/my-documents/upload-document.php on line 53

Notice: Undefined index: type in /var/www/html/dev/my-documents/upload-document.php on line 54

【问题讨论】:

  • multiple 仅适用于支持 html5 的情况,您的浏览器是否支持?
  • fyi,您检查扩展的方式是错误的,很容易被欺骗。你应该检查 mime 类型。提示 finfo 用于 php 5.3+
  • 不要混淆 - 获得“所有这些通知”是向前的进步。您只是未能正确地遍历整个文件数组。 I've added an answer 使用您的代码的精简版本标记的副本。祝你好运。如果有任何不清楚的地方,请在此处联系我 (@HPierce)。

标签: php


【解决方案1】:
 You should 
 <input type="file" name="filesToUpload" id="filesToUpload" multiple> instead of 
<input type="file" name="filesToUpload[]" id="filesToUpload" multiple> 
Array format 

for more info visit [https://stackoverflow.com/questions/2704314/multiple-file-upload-in-php]

【讨论】:

    【解决方案2】:
    <input type="file" name="filesToUpload" id="filesToUpload" multiple>
    

    它应该是数组。把上面的代码改成如下

    <input type="file" name="filesToUpload[]" id="filesToUpload" multiple>
    

    【讨论】:

      猜你喜欢
      • 2015-03-21
      • 1970-01-01
      • 2014-03-23
      • 2022-06-10
      • 2011-04-29
      • 1970-01-01
      • 1970-01-01
      • 2016-04-27
      • 1970-01-01
      相关资源
      最近更新 更多