dousong2967 2015-05-02 13:50
浏览 59
已采纳

如何从单个输入字段上传codeigniter上的多个图像

I want to upload multiple images on my gallery till today I was using this code to upload single images

my controller is

public function addPhoto($album_id)
        {
            if ($this->input->post('submit')) {
                $this->form_validation->set_rules('caption', 'Caption', 'required');
                $this->form_validation->set_rules('userfile', 'Image', 'callback__checkImage');

                if ($this->form_validation->run()) {
                    $config['upload_path'] = $this->config->item('upload_path');
                    $config['allowed_types'] = $this->config->item('allowed_types');
                    $config['max_size'] = $this->config->item('max_size');
                    $config['max_width'] = $this->config->item('max_width');
                    $config['max_height'] = $this->config->item('max_height');
                    $config['encrypt_name'] = $this->config->item('encrypt_name');

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

                    if ($this->upload->do_upload()) {
                        $image = $this->upload->data();

                        $this->album_model->addPhoto($image['file_name'], $album_id);
                        $this->session->set_flashdata('msg', 'Photo added successfully.');
                        redirect('album/photos/'.$album_id);
                    } else {
                        $this->session->set_flashdata('msg', $this->upload->display_errors());
                        redirect('album/addPhoto/'.$album_id);
                    }
                }
            }

            $data['title'] = 'Add Photo';
            $data['content'] = $this->load->view('album/add_photo', '', true);
            $this->load->view('template', $data);
        }

My models is

function addPhoto($image, $album_id)
    {
        $this->db->set('name', $image);
        $this->db->set('caption', $this->input->post('caption'));
        $this->db->set('album_id', $album_id);
        $this->db->insert('photos');
    }

and my view is

<form action="" method="post" enctype="multipart/form-data">
    <h1>Add Photo</h1>
    <?php echo $this->session->flashdata('msg'); ?>

    <div>
        <label>Image</label>
        <input type="file" name="userfile">
        <?php echo form_error('userfile'); ?>
    </div>
    <br>

    <div>
        <label>Caption</label>
        <input type="text" name="caption" value="<?php echo set_value('caption'); ?>" placeholder="Caption">
        <?php echo form_error('caption'); ?>
    </div>
    <br>

    <div>
        <label></label>
        <button type="submit" name="submit" value="submit">Upload</button>
    </div>
</form>

This is what I'm using for uploading single images now I want to upload multiple and I had only one images fields on database did I need to make more images fields for upload multiple images ?

  • 写回答

2条回答 默认 最新

  • doudun1934 2015-05-05 01:11
    关注

    You got to send <input type="file" name="userfile"> as an array like so:

    <input type="file" multiple name="userfile[]">
    

    Then for each posted image do an upload.

    BTW a quick search on this site gave me this post, that has all other info you need: Multiple files upload (Array) with CodeIgniter 2.0

    good luck

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂