I want to multi-upload an images using codeigniter and save it into a table, seems no error shown on my code, but returned no results (aka Not Saved to Database and Images not uploaded)
Here's my code on Controller
public function tambah_proses()
{
$this->load->library('upload');
$dataInfo = array();
$files = $_FILES;
$cpt = count($_FILES['userfile']['name']);
for($i=0; $i<$cpt; $i++)
{
$_FILES['userfile']['name']= $files['userfile']['name'][$i];
$_FILES['userfile']['type']= $files['userfile']['type'][$i];
$_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
$_FILES['userfile']['error']= $files['userfile']['error'][$i];
$_FILES['userfile']['size']= $files['userfile']['size'][$i];
$this->upload->initialize($this->set_upload_options());
$this->upload->do_upload('userfile[]');
$dataInfo[] = $this->upload->data();
}
$data = array(
'pic_id' => $this->input->post('pic_id'),
'img1' => $dataInfo[0]['file_name'],
'img2' => $dataInfo[1]['file_name'],
'img3' => $dataInfo[2]['file_name'],
'img4' => $dataInfo[3]['file_name']
);
$this->Product_model->insert($data);
}
private function set_upload_options()
{
$config = array();
$config['upload_path'] = 'upload/images';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['max_size'] = '2048'; // 2 MB
return $config;
}
on View :
<form action="<?php base_url("admin/products/tambah_proses") ?>" method="post" enctype="multipart/form-data" >
<input type="hidden" name="pic_id" value="<?php echo $product->product_id?>" />
<div class="form-group">
<label for="name">Gambar Tambahan*</label>
<input class="form-control" type="file" name="userfile[]" multiple> <br/><br/>
</div>
<input class="btn btn-success" type="submit" name="btn" value="Save" />
</form>
On Model :
function insert($data)
{
$this->db->insert('prod_pict', $data);
}
Can you tell me what's wrong on my code?
Your code has the following issues: First, include "echo" before the base_url(in form action) like this:
<form action="<?php echo base_url("admin/products/tambah_proses") ?>" method="post" enctype="multipart/form-data" >
Then correct your controller code as follows:
public function tambah_proses()
{
$this->load->library('upload');
$dataInfo = array();
$files = $_FILES;
$cpt = count($_FILES['userfile']['name']);
for($i=0; $i<$cpt; $i++)
{
if($i < 4){
$_FILES['userfile']['name']= $files['userfile']['name'][$i];
$_FILES['userfile']['type']= $files['userfile']['type'][$i];
$_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
$_FILES['userfile']['error']= $files['userfile']['error'][$i];
$_FILES['userfile']['size']= $files['userfile']['size'][$i];
$this->upload->initialize($this->set_upload_options());
$this->upload->do_upload('userfile[]');
$dataInfo[] = $this->upload->data();
}
}
$data = array();
$counter = 1;
foreach($dataInfo as $one){
if ($counter <= 4) {
$index = "img".$counter;
$data[$index] = $one['file_name'];
}
}
$data['pic_id'] = $this->input->post('pic_id');
$this->Product_model->insert($data);
}
Basically your old code would throw an error when the user uploads less than 4 files by accessing it via an absolute index like:
$dataInfo[3]['file_name']
Inorder to prevent this, I looped through the datainfo array such that it executes based on the number of array items held. And since you have atmost 4 images required, the code executes as long as the counter is less or equal to 4.
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.