0

好的,所以我已经搞砸了几个小时,阅读文档和浏览论坛,但我不知道为什么这个上传表单不起作用。

整个表单完美运行,数据保存到数据库和所有内容。但是我只是添加了一个图像上传输入,它不起作用!我已经按照文档中的确切教程以及其他几个教程进行操作。

这是处理表单提交的代码($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;
     }                                  
}

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

完成上传后,我需要将图像名称保存在我的数据库表中,但这不是重点。我只需要让这个实际上传。

已解决——上传目录无效

4

2 回答 2

1

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

尝试将您的 do_upload() 方法 last 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;
 } 

希望这将帮助您找出导致问题的原因。

于 2013-05-24T16:17:56.343 回答
0

我的同样问题,我通过升级 CodeIgniter 解决了这个问题,

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

只需替换根文件夹中的系统文件夹,

之后,问题在实时服务器中解决,

我希望我的建议有用

于 2017-01-11T13:51:28.893 回答