简体   繁体   中英

how to upload multiple image with different input file in codeigniter

I want to upload multiple image with different input file with array name.

view :

<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>

controller:

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();
    }

result in browser always show error that mean return status to error in upload_picture function, I want to get filename that encrypted and store to database like a4b8a0e070128b0a3dabd9e2931f7ae3.jpg not picture.jpg.

在此处输入图片说明

codeigniter upload class doesn't support array file name in this way. So either you have to modify upload class file (not recommended to modify core class file), or you have to modify your codes. You can name the inputs like this: picture_1, picture_2 etc. In that case, modify upload_picture() method like this way:

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; //

This way you are uploading files one by one using the loop. However, you have to also modify main method as now it is returning multidimensional array. I've given you just an idea...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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