如何从单个输入字段上传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 ?
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
If you want to upload multiple images there are so many plugins for that
This a very simple and very great plugin I have also used it in may projects.