1

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?

2
  • What does "not working" mean? Commented Apr 13, 2017 at 14:32
  • Try this : echo json_encode(array('success'=>false,'message'=>$error['error'])); Commented Apr 13, 2017 at 16:47

1 Answer 1

1

$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));
} 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Bikram Pahi. After using your code I am getting error <. What does it mean ?
&lt; used for ( < ) , please check in your IDE you will get if any typo error or left php tags etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.