Codeigniter:文件名未出现在数据库中

时间:2013-09-16 15:14:18

标签: php codeigniter

我只是学习将图像数据保存到数据库,这些是文件名和路径。该路径出现在数据库中,但不出现在文件名中。问题是什么?

这是控制器,

function do_upload() {
    $config['upload_path']='./uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '100';
    $config['max_width'] = '1024';
    $config['max_height'] = '768';

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

    if(!$this->upload->do_upload()){
        $error = array('error' => $this->upload->display_errors());

        $this->load->view('upload_form', $error);
    } else {
        $data  = array('upload_data' => $this->upload->data());

        $this->load->view('upload_success', $data);
    }
}

和模型

    function upload ($config) {
    $config = $this->upload->data();
    $upload_data = array(
        'path' => $config['full_path'],
        'nama_foto' => $config['file_name']
    );

    $this->db->insert('tb_picture', $upload_data);
}

和表格 enter image description here

我该怎么办?

谢谢你。

1 个答案:

答案 0 :(得分:1)

在阅读本文之前,请自行尝试,或尝试使用网络上的任何类型的viedotutorial http://net.tutsplus.com/sessions/codeigniter-from-scratch/

控制器功能应如下所示

function do_upload()
    {
        $config['upload_path']='./uploads/'; //needs to set uploads folder CHMOD 0644
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '100';
        $config['max_width'] = '1024';
        $config['max_height'] = '768';

        $config['overwrite']  = FALSE;
        $config['remove_spaces']  = TRUE;

        $field_name = "userfile"; //name tag in our HTML form in case you want to change it

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

        if ( ! $this->upload->do_upload($field_name)) //upload happens
        {
            $error = array('error' => $this->upload->display_errors());
            $this->load->view('upload_form', $error);
        }    
        else
        {
             //succesful upload get data from upload and use them with our model
             $upload_data = $this->upload->data();
             $this->upload_model->upload($upload_data);
        }
    }    

模型功能

function upload ($data) {

    if (empty($data) || $data === FALSE) return FALSE;

    $insert_data = array(
        'path' => $data['full_path'],
        'nama_foto' => $data['file_name']
    );

   $this->db->insert('tb_picture', $insert_data);

   return $this->db->insert_id(); //returns last inserted ID
}

请注意,您的模型完全“错误”,您在其中使用了上传功能,尝试仅将data传递给它,以便模型可以处理它。