【问题标题】:How to store the file in mysql database using codeigniter?如何使用codeigniter将文件存储在mysql数据库中?
【发布时间】:2016-05-18 21:54:07
【问题描述】:

我创建了一个简单的联系表格。我想将文件存储在任何用户上传的数据库中。我可以将文件存储在我的文件夹中,但我必须将文件存储在数据库中以识别管理员文件正在上传。

控制器

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

 public function do_upload()
        {


                $id = $this->session->userdata('id');
                $this->load->model('Model_doc');

                $config['upload_path']          = './upload/';
                $config['allowed_types']        = 'docx';
                $config['max_size']             = 1000;
                $config['max_width']            = 1024;
                $config['max_height']           = 768;

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

                $this->upload->do_upload();
                $this->Model_doc->index($id, $this->upload->data());
   } 

型号

public function index($id,$imgdata)

{


 $imgdata = file_get_contents($imgdata['../upload/']);

$data = array(
        'path'=>$imgdata,

         );
    $this->db->set('curentDate', 'NOW()', FALSE);
 $this->db->insert('test',$data);

}

查看

<?php echo form_open_multipart('welcome/do_upload');?>

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

<br /><br />

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

</form>

【问题讨论】:

  • 模型 1 中出现 2 个错误)未定义索引:../upload/ 2)file_get_contents():文件名不能为空。

标签: php mysql codeigniter codeigniter-3


【解决方案1】:

将控制器文件中的代码更改为如下:

public function do_upload()
    {
        $id = $this->session->userdata('id');
        $this->load->model('Model_doc');
        $config['upload_path']          = './upload/';
        $config['allowed_types']        = 'docx';
        $config['max_size']             = 1000;
        $config['max_width']            = 1024;
        $config['max_height']           = 768;
        $this->load->library('upload', $config);
        $this->upload->do_upload();

        $full_file_path = base_url()."/upload/".$_FILES['userfile']['name'];
        //This Is Full Path of the file stored to your folder,Change it if required.

        $this->Model_doc->index($id,$full_file_path);
    }

模型文件的变化:

public function index($id,$full_file_path)
    {

        $data = array(
            'path'=>$full_file_path,
        );
        $this->db->set('curentDate', 'NOW()', FALSE);
        $this->db->insert('test',$data);
    }

【讨论】:

  • /newfolder//upload的索引

    /newfolder//上传的索引

  • 感谢 Ketan 先生的回复,当我提交数据时,我在我的数据库中得到了上面的代码....
  • @Hybreeder 你需要存储在数据库中的路径还是只需要文件名,因为 File_get_contents 从提到的起点读取文件作为字符串。
  • @Hybreeder 检查上面的代码,我已经更新了代码。
  • @Hybreeder 继续摇摆...:)
猜你喜欢
  • 1970-01-01
  • 2012-03-17
  • 1970-01-01
  • 2022-10-07
  • 2011-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多