CodeIgniter图像上传不工作,没有错误


CodeIgniter Image Upload Not Working and No Errors

好吧,所以我已经搞乱这个几个小时了,现在,阅读文档和浏览论坛,但我不能找到为什么这个上传表单不工作。

整个表单工作完美,数据保存到DB和一切。但我刚刚添加了一个图像上传输入,它不工作!我遵循了文档中的确切教程,以及其他几个教程。

下面是处理表单submit的代码($this->page_m是我的模型):
public function edit ($id = NULL)
{
    // Fetch a page or set a new one
    if ($id) {
        $this->data['page'] = $this->page_m->get($id);
        count($this->data['page']) || $this->data['errors'][] = 'page could not be found';
    }
    else {
        $this->data['page'] = $this->page_m->get_new();
    }
    // Pages for dropdown
    $this->data['pages_no_parents'] = $this->page_m->get_no_parents();      
    // Process the form
    if ($this->form_validation->run('page_rules') == TRUE) {
        $data = $this->page_m->array_from_post(array(
            'title', 
            'slug', 
            'body', 
            'template', 
            'parent_id'           
        ));                    
        if(!empty($_FILES['userfile'])) {
           $this->do_upload();
        }
        $this->page_m->save($data, $id); 
        redirect('admin/page');         
    }        
    // Load the view
    $this->data['subview'] = 'admin/page/edit';
    $this->load->view('admin/_layout_main', $this->data);
}

,这里是处理照片上传的代码,它在$this->form_validation->run()检查之后被调用:

//upload a slider photo
public function do_upload() {                   
     $config = array(
         'allowed_types' => 'jpg|jpeg|gif|png',
         'upload_path' => site_url('uploads/')
     );       

     $this->load->library('upload');
     $this->upload->initialize($config);
     $field_name = "userfile";
     if ( ! $this->upload->do_upload($field_name)) {
             $this->data['error'] = array('error' => $this->upload->display_errors());
             $this->data['subview'] = 'admin/page/edit';
             $this->load->view('admin/_layout_main', $this->data);
     } else {
         return true;
     }                                  
}

为了调试的目的,我故意使这个上传脚本尽可能简单和基本,但是我仍然没有成功地让这个表单正确上传。

在我得到上传工作后,我需要在我的数据库表中保存图像名称,但这不是现在的重点。我只需要让它真正上传。

SOLVED——无效上传目录

要调试您的脚本,您应该尝试回显$this->upload->display_errors();返回的值

尝试更改do_upload()方法的最后if{} else{}语句:

 if ( ! $this->upload->do_upload($field_name)) {
         // return the error message and kill the script
         echo $this->upload->display_errors(); die();
         $this->data['error'] = array('error' => $this->upload->display_errors());
         $this->data['subview'] = 'admin/page/edit';
         $this->load->view('admin/_layout_main', $this->data);
 } else {
     return true;
 } 

希望这能帮助你找出问题的原因。

与我的问题相同,我通过升级CodeIgniter来解决这个问题,

我的旧版本是3.1.2,然后我刚升级到CI-3.1.3,

只是替换根目录下的system文件夹,

之后,问题在服务器上解决,