【问题标题】:Error handling for ajax file uploading with CodeIgniter使用 CodeIgniter 上传 ajax 文件的错误处理
【发布时间】:2013-07-26 09:35:56
【问题描述】:

使用 CodeIgniter 2.1.4,我正在尝试使用 ajax 和文件上传类上传文件。我在控制器中的构造为:

public function __construct()
{
    parent::__construct();
    $this->load->helper(array('form', 'url'));
}

而我在Controller中的上传功能为:

public function upload_file()
{

    $config['upload_path'] = 'upload/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '100';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';

    $this->load->library('upload', $config);
    if (!$this->upload->do_upload())
    {
        $error = array('error' => $this->upload->display_errors());
        $result['success'] = 1;
        $result['message'] = $error;
    }
    else
    {
        $result['success'] = 0;
        $result['message'] = "Successful Upload";
    }

    die(json_encode($result));

}

但是出现的错误是:

Array

如何从数组中获取错误消息?

【问题讨论】:

    标签: php ajax codeigniter file-upload


    【解决方案1】:

    换行试试:

    $result['message'] = $error['error'];
    

    【讨论】:

      【解决方案2】:

      发生错误时,您的结果是一个数组。您在 die 函数中使用了 json 编码。

      不要在 die 函数中编码。

      echo json_encode($result);
      die();
      

      【讨论】:

      • 我不知道这有什么不同
      • 我不知道是什么问题。您是否使用 print_r 来解决问题?使用带有上传功能的文件字段名称。应该是这个问题。
      猜你喜欢
      • 2017-10-22
      • 2017-08-16
      • 1970-01-01
      • 2012-05-25
      • 2012-07-18
      • 1970-01-01
      • 2016-11-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多