Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用codeigniter将文件路径插入数据库_Php_Mysql_Database_Codeigniter_File Upload - Fatal编程技术网

Php 如何使用codeigniter将文件路径插入数据库

Php 如何使用codeigniter将文件路径插入数据库,php,mysql,database,codeigniter,file-upload,Php,Mysql,Database,Codeigniter,File Upload,我在使用Codeigniter将文件路径插入数据库时遇到问题,我有一个表单字段,用户可以在其中上载任何jpg | png | gif文件,上载的文件成功发送到上载目录,并显示成功消息。但我将如何获得上传的特定文件的文件路径并将其插入数据库 在我调用模型之前,下面的代码工作正常: `$this->upload_model->upload_path();` 所以我的模型肯定有问题,我想不出来 这是我的控制器: class Upload extends CI_Controller {

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

在我调用模型之前,下面的代码工作正常:

`$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);

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

观点:

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

上传表单



创建一个数组,其中包含您需要上传到数据库中的任何内容…然后将其插入数据库,并在“其他”下放置类似的内容

//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)`谢谢你,巴拉根,代码运行得很好,我已经工作了一整天来运行这篇文章…再次感谢你
//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