文件上传与添加链接到数据库
问题描述:
我想如果一个文件被从输入文件标签文件上传与添加链接到数据库
<input name="file1" type="file" id="addimage1">
我想知道如何处理它在车型选择上载文件在一个特定的文件。我想将图像的链接发送到数据库。
当我不使用代码点火我这样做:
if(!empty($_FILES['file1']['name'])) {
move_uploaded_file($_FILES["file1"]["tmp_name"],'assets/results/'.$myid.'/'. $_FILES["file1"]["name"]);
$link1='assets/results/'.$myid .'/' . $_FILES["file1"]["name"];
}
我怎样才能做到这一点的代码点火器。
答
随着用户指南指出:
//after uploading config
if (! $this->upload->do_upload('userfileinputname'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
//here add to db
$this->file_model->add_link_to_db($data['filename']);
$this->load->view('upload_success', $data);
}
}
而且你的模型中:
public function add_link_to_db($filename) {
return $last_id = $this->db->insert('mytable', ['name'=>$filename]);
}
阅读文档https://www.codeigniter.com/userguide3/libraries/file_uploading.html –