【问题标题】:codeigniter file uploading without using array不使用数组上传codeigniter文件
【发布时间】:2015-11-20 00:18:12
【问题描述】:

我使用过codeigniter文件上传,单个和多个。但是如果文件名不在数组中,如何上传文件。例如:- 如果 form1 有 file1、file2 和 file3 文件输入,并且用户只提供 file1 和 file3。那么如何识别是否没有给出file2。如果我使用数组格式,则无法找到给定的特定字段和不给定的特定字段。

我尝试过基于数组的上传,但是任何文件丢失都会给我一个错误索引的数组。我需要有一个适当的$_POST,其中明确给出了未选择的表单成员。

【问题讨论】:

  • 请出示您的代码...

标签: php arrays codeigniter file-upload


【解决方案1】:

它只会发布有价值的$_FILES

HTML

<input type="file" name="mass_consume_upload[]" />
<input type="file" name="mass_consume_upload[]" />
<input type="file" name="mass_consume_upload[]" />

PHP (Codeigniter)

        $this->load->library('upload');

        $tempfiles = $_FILES;
        //print_r($tempfiles);
        foreach ($tempfiles as $files) {
            $_FILES['mass_consume_upload'] = $files;

            $this->upload->initialize(array(
                'upload_path' => $path,
                'allowed_types' => 'pdf',
                'overwrite' => FALSE
            ));
            if (!$this->upload->do_upload('mass_consume_upload')) {
                $uploadArr['error'][] = array(
                    'filename' => $_FILES['mass_consume_upload']['name'],
                    'msg' => $this->upload->display_errors('', '')
                );
            } else {
                $uploadArr['success'][] = array('filename' => $_FILES['mass_consume_upload']['name']);
            }
        }

【讨论】:

  • 真的是大罪。当未选择文件时,我忘记了文件输入中的错误值。谢谢你的解决方案。同时,我想到了另一种解决方案,即我在表单之前下载图像,并从数据库中获取文件 id,并将 id 放置在原始表单中。但您的解决方案更优雅。谢谢
  • 很高兴,如果我能帮助你。
猜你喜欢
  • 1970-01-01
  • 2012-07-16
  • 1970-01-01
  • 2015-06-10
  • 2015-12-26
  • 2019-05-18
  • 2011-11-15
相关资源
最近更新 更多