【问题标题】:Codeigniter multiple images upload, get each filenameCodeigniter 多张图片上传,获取每个文件名
【发布时间】:2013-02-10 20:33:00
【问题描述】:

我正在使用下面的代码来处理图片上传。每件事都进展顺利。我允许上传两张图片的选项,一张是徽标,另一张是屏幕截图。

现在,当我提供编辑上传的选项时,比如选择要上传的新文件并替换旧文件,我遇到了这个问题。

在我的images[]数组中,我会在循环的帮助下一一获取图片名称。但是,当用户想要更改第二张图片(屏幕截图)并且在徽标字段中没有选择任何内容时,应该只更改屏幕截图。

但在images[] 数组中,第一个元素将是上传的第一个元素。如果我选择两个图像都可以,我将在images[0] 中获得 logo.jpg 和在images[1] 中获得 screenshot.jpg。但是当我只选择第二张图片时,我会在images[0] 中得到 screenshot.jpg。但是,数组的第一个元素应该是空白的,并且数组的第二个元素应该具有第二个图像上传选项的数据,以便更改生效。我怎样才能做到这一点?

$config['upload_path'] = '../uploads/';
            $config['allowed_types'] = '*';
            $config['max_size'] = '0';
            $config['max_width']  = '0';
            $config['max_height']  = '0';

            $this->load->library('upload', $config);
            $image = array();
            foreach ($_FILES as $key => $value) {

                if (!empty($value['tmp_name'])) {

                    if ( ! $this->upload->do_upload($key)) {
                        $error = array('error' => $this->upload->display_errors());
                        //failed display the errors
                    } else {
                        //success
                        $data =  $this->upload->data();
                            $image[]= $data['file_name'];
                    }

                }
           }

【问题讨论】:

    标签: php html codeigniter image-upload


    【解决方案1】:

    请记住,您必须在每次上传时初始化上传库。

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

    用户指南:http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html

    编辑:

    你可以检查$_FILES数组,看看是否有截图输入的名字和logo输入,如果没有,你可以创建你需要的数组

    我发现了这个:

    创建该数据结构的更简单方法是命名您的 HTML 文件输入不同的名称。 >如果要上传多个文件,请使用:

    <input type=file name=file1>
    <input type=file name=file2>
    <input type=file name=file3>
    

    每个字段名都是 $_FILES 数组中的一个键。

    http://www.php.net/manual/es/features.file-upload.multiple.php#53933

    【讨论】:

      猜你喜欢
      • 2020-12-11
      • 2021-09-04
      • 1970-01-01
      • 2017-04-08
      • 2011-12-10
      • 2017-07-31
      • 2016-01-06
      • 1970-01-01
      相关资源
      最近更新 更多