two different field image upload to different location in codeigniter code example
Example 1: how to upload two files on same form different path in codeigniter
if($this->input->post('Submit')){
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'jpg|jpeg|bmp|png';
$config['max_size'] = '30720';
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
$this->upload->do_upload('userfile');
$video_upload=$this->upload->data();
$config2['upload_path'] = './thumb/';
$config2['allowed_types'] = 'jpg|jpeg|bmp|png';
$config2['max_size'] = '30720';
$config2['encrypt_name'] = TRUE;
$this->upload->initialize($config2);
$this->upload->do_upload('txt_thumb');
$thumbnail_upload=$this->upload->data();
$date=date("d-m-Y");
$data = array(
'parent_id'=> $this->input->post('txt_parent'),
'cat_id' => $this->input->post('txt_category'),
'title'=> $this->input->post('txt_title'),
'status' => $this->input->post('txt_status'),
'featured' => $thumbnail_upload['file_name'],
'image' => $video_upload['file_name'],
'time'=>$date
);
$sql_ins= $this->Insimage->insertimage($data);
if($sql_ins)
{
$data['Success'] = "Image has been succesfully inserted!!";
}
Example 2: Multiple image upload with CodeIgniter
views
---------
<?php echo validation_errors();?>
<?php echo form_open_multipart('pages/multiple_files');?>
<p><input type="file" multiple="multiple" name="image_name[]" class="form-control" /></p>
<input type="submit" class="btn btn-success btn-block"/>
</form>
Controller
----------
public function multiple_files(){
$this->load->library('upload');
$image = array();
$ImageCount = count($_FILES['image_name']['name']);
for($i = 0; $i < $ImageCount; $i++){
$_FILES['file']['name'] = $_FILES['image_name']['name'][$i];
$_FILES['file']['type'] = $_FILES['image_name']['type'][$i];
$_FILES['file']['tmp_name'] = $_FILES['image_name']['tmp_name'][$i];
$_FILES['file']['error'] = $_FILES['image_name']['error'][$i];
$_FILES['file']['size'] = $_FILES['image_name']['size'][$i];
// File upload configuration
$uploadPath = './assets/images/profiles/';
$config['upload_path'] = $uploadPath;
$config['allowed_types'] = 'jpg|jpeg|png|gif';
// Load and initialize upload library
$this->load->library('upload', $config);
$this->upload->initialize($config);
// Upload file to server
if($this->upload->do_upload('file')){
// Uploaded file data
$imageData = $this->upload->data();
$uploadImgData[$i]['image_name'] = $imageData['file_name'];
}
}
if(!empty($uploadImgData)){
// Insert files data into the database
$this->pages_model->multiple_images($uploadImgData);
}
}
Model
---------
public function multiple_images($image = array()){
return $this->db->insert_batch('profile_images',$image);
}