codeigniter没有显示文件上传错误

时间:2014-11-30 10:53:05

标签: php codeigniter file-upload error-handling

我正在使用CodeIgniter文件上传类来上传图片。但是,如果我尝试选择pdf而不是图像,我在表单提交时不会出现任何错误。

相关代码

$config = array(
    'allowed_types' => 'jpg|jpeg|gif|png',
    'upload_path'   => $this->article_path.'/magazine',
    'max_size'      => 2000
);

$this->load->library('upload', $config);
$this->upload->do_upload();
$this->upload->display_errors();
$image_data = $this->upload->data();

1 个答案:

答案 0 :(得分:0)

因为您从未访问过文档(here),所以请这样做才能在CodeIgniter框架中表现更好。


这应该让你抬头(请阅读评论)

$config = array(
    'allowed_types' => 'jpg|jpeg|gif|png',
    'upload_path'   => $this->article_path.'/magazine',
    'max_size'      => 2000
);

$this->load->library('upload', $config);

if ( ! $this->upload->do_upload()) //important!
{
    // something went really wrong show error page
    $error = array('error' => $this->upload->display_errors()); //associate view variable $error with upload errors

    $this->load->view('upload_form', $error); //show error page
}
else
{
    //all is good we upload
    $data = array('upload_data' => $this->upload->data()); 

    $this->load->view('upload_success', $data);
}
相关问题