【问题标题】:How to insert file_path into database using codeigniter如何使用 codeigniter 将 file_path 插入数据库
【发布时间】:2015-05-29 10:42:41
【问题描述】:

我在使用 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>

【问题讨论】:

    标签: php mysql database codeigniter file-upload


    【解决方案1】:

    使用您需要上传到数据库中的任何内容创建一个数组...然后将其插入数据库中,在 else 下放置类似这样的内容

    //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
    

    【讨论】:

    • 我需要在最后一行添加:`return $this->db->insert('photos', $insert_data);`
    • 谢谢 Parangan ,代码运行良好,我一直在工作一整天来运行这篇文章...再次感谢您
    猜你喜欢
    • 2014-09-24
    • 2013-04-04
    • 2016-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多