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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?