如何使用 codeigniter 将文件存储在 mysql 数据库中?
How to store the file in mysql database using codeigniter?
我创建了一个简单的联系人 form.I 想将文件存储在由任何人上传的数据库中 user.I 我可以将文件存储在我的文件夹中,但我必须将文件存储在数据库以识别正在上传哪个文件的管理员。
控制器
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>
将控制器文件中的代码更改为如下:
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);
}
我创建了一个简单的联系人 form.I 想将文件存储在由任何人上传的数据库中 user.I 我可以将文件存储在我的文件夹中,但我必须将文件存储在数据库以识别正在上传哪个文件的管理员。
控制器
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>
将控制器文件中的代码更改为如下:
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);
}