如何使用codeigniter将file_path插入数据库

时间:2015-03-25 16:24:08

标签: php mysql database codeigniter file-upload

我遇到使用Codeigniter将file_path插入数据库的问题,我有一个表单字段,用户可以上传任何jpg | png | gif文件,上传的文件成功发送到上传目录,并显示成功消息。但是,如何上传该特定文件的file_path并将其插入数据库。

以下代码正常工作,直到我调用模型:

`$this->upload_model->upload_path();`

如此肯定我的模型存在问题,无法弄清楚。

这是我的控制器:

class Upload extends CI_Controller {

        public function __construct()
        {
                parent::__construct();
                $this->load->helper(array('form', 'url'));
                $this->load->model('upload_model');
        }

        public function index()
        {
                $this->load->view('upload_form', array('error' => ' ' ));
        }

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

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

                        // when the code below is uncommented it shows error,otherwise runs
                        // $this->upload_model->upload_path();

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

                }
        }
}

这是我的模型(显示错误):

class Upload_model extends CI_Model {

// this function will be called instantenneously

    public function __construct()
    {
        parent::__construct();

        $this->load->database();
    }
    public function upload_path(){

// ::::::::UNDER CONSTRUCTION::::::::::

        $data = array(
            'path' =>$upload_data['full_path'],

            );

        return $this->db->insert('upload', $data);


    }

}

视图:

<html>
<head>
    <title>Upload Form</title>
</head>
<body>

    <?php echo $error;?>

    <form method="POST" action="/sandeep/ci/index.php/upload/do_upload" enctype="multipart/form-data" />

    <input type="file" name="userfile" size="20" />

    <br /><br />

    <input type="submit" value="upload" />

</form>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

创建一个数组,其中包含您需要在数据库中上传的内容...然后将其插入数据库中,在其他地方插入

//create array to load to database
              $image_data = $this->upload->data();
                $insert_data = array(
                    'name' => $image_data['file_name'],
                    'path' => $image_data['full_path'],
                    'thumb_path'=> $image_data['file_path'] . 'thumbs/'. $image_data['file_name'],
                    'tag' => $tag
                     );

          $this->db->insert('photos', $insert_data);//load array to database