Multiple files upload (Array) with CodeIgniter
view:
<input type="file" class="form-control" name="userfile[]" multiple>
controller:
function insertuser()
{
$this->load->library('upload');
$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());
$uploadimages=$this->upload->do_upload();
$dataInfo[] = $this->upload->data();
}
/*insert database */
$data = array(
'name' => $this->input->post('pd_name'),
'prod_image' => $dataInfo[0]['file_name'],
'prod_image1' => $dataInfo[1]['file_name'],
'prod_image2' => $dataInfo[2]['file_name'],
'created_time' => date('Y-m-d H:i:s')
);
$result_set = $this->tbl_products_model->insertUser($data);
/*end insert database*/
if($uploadimages){
echo "upload";
}else{
echo "error";
$error = array('error' => $this->upload->display_errors());
print_r($error);
}
}
private function set_upload_options()
{
$config = array();
$config['upload_path'] = './assent/images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['overwrite'] = FALSE;
return $config;
}
Config By: https://stackoverflow.com/a/40778817/6684853
<input type="file" class="form-control" name="userfile[]" multiple>
controller:
function insertuser()
{
$this->load->library('upload');
$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());
$uploadimages=$this->upload->do_upload();
$dataInfo[] = $this->upload->data();
}
/*insert database */
$data = array(
'name' => $this->input->post('pd_name'),
'prod_image' => $dataInfo[0]['file_name'],
'prod_image1' => $dataInfo[1]['file_name'],
'prod_image2' => $dataInfo[2]['file_name'],
'created_time' => date('Y-m-d H:i:s')
);
$result_set = $this->tbl_products_model->insertUser($data);
/*end insert database*/
if($uploadimages){
echo "upload";
}else{
echo "error";
$error = array('error' => $this->upload->display_errors());
print_r($error);
}
}
private function set_upload_options()
{
$config = array();
$config['upload_path'] = './assent/images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['overwrite'] = FALSE;
return $config;
}
Config By: https://stackoverflow.com/a/40778817/6684853
Comments
Post a Comment