【问题标题】:Looped file array not uploading all files循环文件数组未上传所有文件
【发布时间】:2019-12-13 02:18:18
【问题描述】:

我正在尝试根据用户上传的内容上传 1 - x 数量的文件。最近,我创建了一个 for 循环来遍历文件。我看到的只是第一个文件上传。

有人知道为什么只上传第一个文件吗?

class fileUpload
{

    public function __construct()
    {}
    public function upload() {

        $file_count = count($_FILES['uploadedFile']['name']);
        //$file_count = count($_FILES($file_post['name']));

        for ($i = 0; $i<$file_count; $i++) {
//          echo $file['uploadedFile']['name'][$index] . "\n";

            $target_dir = "uploads/";
            $target_file = $target_dir . basename($_FILES["uploadedFile"]["name"][$i]);
            $uploadOk = 1;
            $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));        

            // Check if $uploadOk is set to 0 by an error
            if ($uploadOk == 0) {
                return 0;
    // if everything is ok, try to upload file
            } else {
                if (move_uploaded_file($_FILES["uploadedFile"]["tmp_name"][$i], $target_file)) {
                    return basename($_FILES["uploadedFile"]["name"][$i]);
                } else {
                    return 0;
                }
            }
        }

    }
}

表单输入:

&lt;input type="file" name="uploadedFile[]" class="inputfile" id="uploadedFileTest" data-multiple-caption="{count} files selected" multiple&gt;

【问题讨论】:

  • 请同时出示您的表格,您是如何提交表格的,您是说其他表格中的错误索引不为0?
  • 输入添加到问题中。上传时我没有收到任何记录的错误。第一次上传文件后,附加文件不会移动到上传文件夹。

标签: php file for-loop file-upload


【解决方案1】:

那是因为你使用了returnreturn 会让你退出这个功能,你只会得到第一个上传的项目。

一个简单的解决方法是:

if (!move_uploaded_file($_FILES["uploadedFile"]["tmp_name"][$i], $target_file)) {
    return 0;
}

//更新2个解决方案建议

public function upload() {

        $file_count = count($_FILES['uploadedFile']['name']);
        //$file_count = count($_FILES($file_post['name']));
            // add a array to save the basename on each loop
            $results = [];
        for ($i = 0; $i<$file_count; $i++) {
//          echo $file['uploadedFile']['name'][$index] . "\n";

            $target_dir = "uploads/";
            $target_file = $target_dir . basename($_FILES["uploadedFile"]["name"][$i]);
            $uploadOk = 1;
            $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));        

            // Check if $uploadOk is set to 0 by an error
            if ($uploadOk == 0) {
                return 0;
    // if everything is ok, try to upload file
            } else {
                if (move_uploaded_file($_FILES["uploadedFile"]["tmp_name"][$i], $target_file)) {
                    $results[] = basename($_FILES["uploadedFile"]["name"][$i]);
                } else {
                    return 0;
                }
            }
        }
        //return all basename in one shot
        return $results;

    }

【讨论】:

  • continue 可能更合适,具体取决于 OP 是否要继续处理其他上传。
  • 嗯,后面的其他事情我需要返回值。完全运行后如何返回?
  • 您可以将每个循环的结果推送到一个数组中,并在循环结束时返回这个数组。
  • 您能否举例说明您的意思?
  • 感谢您的示例。出于某种原因,我肯定是想多了。文件现在正确上传。但是,我收到一条错误通知,我的文件中稍后有一段代码现在只是读取“结果”而不是文件名。这段代码”$fu = new fileUpload(); $filename = $fu-&gt;upload(); $template = str_replace("{filename}", "A file was uploaded. You can download the file from: &lt;a href='php/uploads/{$filename}'&gt;{$filename}&lt;/a&gt;", $template); 只是读取为“结果”并链接到一个名为该页面的页面。如何获取实际文件名来popualte?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-06
  • 1970-01-01
  • 1970-01-01
  • 2015-05-31
相关资源
最近更新 更多