RebDev
RebDev

Reputation: 335

Error handling for ajax file uploading with CodeIgniter

With CodeIgniter 2.1.4 I'm trying to upload a file using ajax and the File Uploading Class. I have the construct in the Controller as:

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

And my upload function in the Controller as:

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));

}

But the errors are coming out as:

Array

How can I get the error message out of the array?

Upvotes: 0

Views: 511

Answers (2)

Dinuka Thilanga
Dinuka Thilanga

Reputation: 4330

When error has occurred your result is a array. You used json encode in die function.

Don't encode in die function.

echo json_encode($result);
die();

Upvotes: 0

Nil'z
Nil'z

Reputation: 7475

Change this line and try:

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

Upvotes: 1

Related Questions