1

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

这是控制器,

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);
}

和桌子 在此处输入图像描述

我应该怎么办?

谢谢你。

4

1 回答 1

1

在阅读本文之前,请自己再试一次,或尝试网络上的任何类型的视频教程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仅传递给它,以便模型可以处理它。

于 2013-09-16T15:35:58.837 回答