$ _FILES数组只存储一个文件PHP

时间:2017-11-14 03:38:00

标签: php

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

<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时,它只显示其中一个文件的名称。我不确定,但我觉得这就是为什么我在尝试上传多个文件时只能将一个文件上传到服务器。

有人可以确认这是否是问题?如果是这样,那么最好的方法就是让它发挥作用。

编辑:我已尝试过在网站上提出的所有其他建议。当我使用<input type="file" name="filesToUpload[]" id="filesToUpload" multiple>时,它就会破坏它。我得到所有这些关于未定义索引的注意事项我想你在试图打破文件的不同部分时称之为扩展(基于下面的人所说的)。我也收到了关于Array to String转换的通知。使用输入中的括号也只是跳转到关于无法上传某种类型的文件的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

2 个答案:

答案 0 :(得分:0)

 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]

答案 1 :(得分:0)

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

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

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