繁体   English   中英

如何在Codeigniter中使用不同的输入文件上传多个图像

[英]how to upload multiple image with different input file in codeigniter

我想上传具有不同输入文件和数组名称的多个图像。

查看:

<form action="" enctype="multipart/form-data" method="post">
<input name="picture[]" class="form-control" style="padding-top: 0;" type="file"/>
<input name="picture[]" class="form-control" style="padding-top: 0;" type="file"/>
<input type='submit' value="upload" />
</form>

控制器:

public function index($id=null)
    {
        $id = $this->input->get('id');
        if ($_POST)
        {
            if ($this->validation()) 
            {
                $file = $this->upload_picture();
                if ($file['status'] == 'success')
                {
                    echo $this->upload->data('file_name');
                }
                else
                {
                    echo $file['data'];
                    echo $file['status'];
                    $this->session->set_flashdata('alert', alert('error', $file['data']));
                }
            }
            else
            {
                $this->session->set_flashdata('alert', alert('error', validation_errors()));
            }
            //redirect($this->agent->referrer());
        }
    }

    private function upload_picture()
    {
        $config['upload_path']      = './assets/img/page/';
        $config['allowed_types']    = 'jpg|png|gif|jpeg';
        $config['max_size']         = 125000; // 1 GB
        $config['encrypt_name']     = TRUE;
        $this->load->library('upload', $config);
        $this->upload->initialize($config);
        if ( ! $this->upload->do_upload('picture[]'))
        {
            return array(
                'status'    => 'error',
                'data'      => $this->upload->display_errors()
                );
        }
        else
        {
            $data                       = $this->upload->data();
            $resize['image_library']    = 'gd2';
            $resize['source_image']     = './assets/img/page/'.$data['file_name'];
            $resize['maintain_ratio']   = TRUE;
            // $resize['width']         = 1920;
            // $resize['height']            = 1080;
            $this->load->library('image_lib', $resize);
            $this->image_lib->resize();
            return array(
                'status'    => 'success',
                'data'      => $this->upload->data()
                );
        }
    }

    private function validation()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('picture[0]',         'Picture',  'trim');
        $this->form_validation->set_rules('picture[1]',         'Picture',          'trim');
        $this->form_validation->set_error_delimiters('',    '<br>');
        return $this->form_validation->run();
    }

浏览器中的结果总是显示错误,这意味着在upload_picture函数中返回错误状态,我想获取加密的文件名并像a4b8a0e070128b0a3dabd9e2931f7ae3.jpg而不是picture.jpg那样存储到数据库。

在此处输入图片说明

codeigniter上传类不以这种方式支持数组文件名。 因此,要么必须修改上载类文件(不建议修改核心类文件),要么必须修改代码。 您可以将输入命名为:picture_1,picture_2等。在这种情况下,请按照以下方式修改upload_picture()方法:

foreach($_FILES as $key=>$val){
    if(!$this->upload->do_upload($key)){
        $return[$key] = $this->upload->display_errors(); //store this in an array and return at the end. array structure is up to you
    }else{
        $return[$key] = $this->upload->data(); //store this in an array and return at the end. array structure is up to you
    }
}

return $return; //

这样,您可以使用循环逐个上传文件。 但是,您还必须修改main方法,因为它正在返回多维数组。 我只是给你一个主意...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM