Reputation: 7022
I am working on image upload in CodeIgniter. In this regard I am using following code.
$config['upload_path'] = './files/';
$config['allowed_types'] = 'gif|jpg|png|doc|txt';
$this->load->library('upload', $config);
if (!$this->upload->do_upload('userfile'))
{
$error = $this->upload->display_errors();
echo json_encode(array('success'=>false,'message'=>$error)); //this line is not working.
}
else
{
echo json_encode(array('success'=>true,'message'=>lang('items_successful_updating').' '. $item_data['name'],'item_id'=>$item_id));
}
If I use below code then it is working.
echo json_encode(array('success'=>false,'message'=>'abcdef'));
Why it is happening so?
Upvotes: 1
Views: 82
Reputation: 1183
$this->upload->display_errors();
returns error array with error code and error message etc. Json Encoding may cause issue sometime in case of multidimensional array. you can use error message only, what I guess from you coding structure.
Correct Way is:
$config['upload_path'] = './files/';
$config['allowed_types'] = 'gif|jpg|png|doc|txt';
$this->load->library('upload', $config);
if (!$this->upload->do_upload('userfile'))
{
$error = $this->upload->display_errors();
echo json_encode(array('success'=>false,'message'=>$error['error'])); //this line is not working.
}
else
{
echo json_encode(array('success'=>true,'message'=>lang('items_successful_updating').' '. $item_data['name'],'item_id'=>$item_id));
}
Upvotes: 1