I need some help. I want to upload two different files in two different directory by using codeigniter. I wrote the following code in my controller. but it will upload only the first image.
public function save_product() {
$data = array();
$error = array();
$config1['upload_path'] = './manager/images/products/';
$config1['allowed_types'] = 'gif|jpeg|png|jpg';
$config1['max_size'] = '3000000';
$config1['max_width'] = '1024';
$config1['max_height'] = '768';
$this->load->library('upload', $config1);
$config2['upload_path'] = './manager/images/products/large/';
$config2['allowed_types'] = 'gif|jpeg|png|jpg';
$config2['max_size'] = '3000000';
$config2['max_width'] = '1024';
$config2['max_height'] = '768';
$this->load->library('upload', $config2);
if ((!$this->upload->do_upload('product_small_image')) && (!$this->upload->do_upload('product_large_image'))){
$error = array('error' => $this->upload->display_errors());
echo "<pre>";
print_r($error);
exit();
}
else {
$fdata = $this->upload->data();
$data['product_small_image'] = 'manager/images/products/' . $fdata['file_name'];
$data['product_large_image'] = 'manager/images/products/large/' . $fdata['file_name'];
$data['product_id'] = $this->input->post('product_id', TRUE);
$data['product_name'] = $this->input->post('product_name', TRUE);
$data['category'] = $this->input->post('category', TRUE);
$result = $this->super_admin_model->save_product_detail($data);
$sdata = array();
$sdata['message'] = "Well done!</strong> You successfully add the Product Details.";
$this->session->set_userdata($sdata);
redirect('super_admin/add_product', 'refresh');
}
}
First, loading the library again with $config2 won't work because the library is already loaded once and $config1 will stay loaded. To load a new config use $this->upload->initialize($config2);
Second, loading $config2 will overwrite the previous config. You should re-arrange your code. Otherwise both uploads will only use the latest config ($config2). Example:
Here is the total code what i wrote to upload multiple image in different directory successfully. and thanks to @Samutz again.
public function save_product() {
$data = array();
$error = array();
$config1['upload_path'] = './manager/images/products/';
$config1['allowed_types'] = 'gif|jpeg|png|jpg';
$config1['max_size'] = '3000000';
$config1['max_width'] = '1024';
$config1['max_height'] = '768';
$this->load->library('upload', $config1);
if (!$this->upload->do_upload('product_small_image')){
$error = array('error' => $this->upload->display_errors());
echo "<pre>";
print_r($error);
exit();
}
else {
$fdata = $this->upload->data();
$data['product_small_image'] = 'manager/images/products/' . $fdata['file_name'];
}
$config2['upload_path'] = './manager/images/products/large/';
$config2['allowed_types'] = 'gif|jpeg|png|jpg';
$config2['max_size'] = '3000000';
$config2['max_width'] = '1024';
$config2['max_height'] = '768';
$this->upload->initialize($config2);
if (!$this->upload->do_upload('product_large_image')){
$error = array('error' => $this->upload->display_errors());
echo "<pre>";
print_r($error);
exit();
}
else {
$fdata = $this->upload->data();
$data['product_large_image'] = 'manager/images/products/large/' . $fdata['file_name'];
}
$data['product_id'] = $this->input->post('product_id', TRUE);
$data['product_name'] = $this->input->post('product_name', TRUE);
$data['category'] = $this->input->post('category', TRUE);
$result = $this->super_admin_model->save_product_detail($data);
$sdata = array();
$sdata['message'] = "Well done!</strong> You successfully add the Product Details.";
$this->session->set_userdata($sdata);
redirect('super_admin/add_product', 'refresh');
}