简体   繁体   English

CodeIgniter 中的图片上传问题

[英]Image upload issue in CodeIgniter

I am working on image upload in CodeIgniter.我正在 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?为什么会这样?

$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.返回带有错误代码和错误消息等的错误数组。在多维数组的情况下,Json 编码有时可能会导致问题。 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));
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM