无法保存已上传的数据库图像的文件路径

时间:2014-04-29 20:54:39

标签: codeigniter image-upload

我的文件已经上传到我的本地文件夹“uploads”,它是root用户但是我无法保存到数据库的文件路径我已经回显了它给出的最后一个查询“0”而不是“filename”这里是我的代码片段'我知道我哪里错了

public function edit($id = NULL) {
        //Fetch a page or set 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();
           }
        $id == NULL || $this->data['page'] = $this->page_m->get($id);
        //Pages for dropdown
        $this->data['pages_no_parents'] = $this->page_m->get_no_parents();
        //dump($this->data['pages_no_parents']);
        //Setup form
        $rules = $this->page_m->rules;

        $this->form_validation->set_rules($rules);
        //Process the form
        if ($this->form_validation->run() == TRUE) {

            $data = $this->page_m->array_from_post(array(
                'title',
                'slug',
                'order',
                'body',
                'template',
                'parent_id',
                'filename'
                ));

      if(!empty($_FILES['filename'])) {
           $this->do_upload('filename');
        }
            $this->page_m->save($data,$id);
            echo '<pre>'.$this->db->last_query().'</pre>';

            redirect('admin/page');
        }
        //Load the view    
        $this->data['subview'] = 'admin/page/edit';
        $this->load->view('admin/_layout_main', $this->data);
    }
    public function do_upload() {                   

     $config = array(
         'allowed_types' => 'jpg|jpeg|gif|png',
         'upload_path' => 'uploads/'
     );


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

     $field_name = 'filename';

     if ( ! $this->upload->do_upload($field_name)) {
            //  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;
     }                                  
}
    public function delete($id) {
        $this->page_m->delete($id);
        redirect('admin/page');
    }
    public function _unique_slug($str) {
        //Do Not validate if slug already exists
        //Unless its slug of the current page
        $id = $this->uri->segment(4);
        $this->db->where('slug', $this->input->post('slug'));
        !$id || $this->db->where('id !=', $id);
        $page = $this->page_m->get();
        if (count($page)) {
            $this->form_validation->set_message('_unique_slug', '%s should be unique');
            return FALSE;
        }
        return TRUE;
    }

1 个答案:

答案 0 :(得分:1)

在do_upload函数中尝试这样做

 if ( ! $this->upload->do_upload($field_name)) {
        //  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 {
        $fInfo = $this->upload->data(); 
        return $fInfo['full_path'];
 } 

in

if(!empty($_FILES['filename'])) {
           $full_path=$this->do_upload('filename');
        }

将$ full_path存储到数据库