如何从类型为blob的数据库下载文件。我已经将文件从图像保存到blob了。但我不知道再次进入形象。
我已经创建了功能下载但没有工作..
插入功能
public funtion upload(){
$name = $_FILES['file']['name'];
$size = $_FILES['file']['size'];
$type = $_FILES['file']['type'];
$from = $_FILES['file']['tmp_name'];
//need to get the content of the file
$fp = fopen($from, 'r');
$file_content = fread($fp, filesize($from));
$file_content = bin2hex($file_content);
fclose($fp);
$insertFile = "insert into tb_file (name,content,type,size) VALUES ('$name','$file_content','$type','$size')";
}
下载功能
public function download(){
$this->load->helper('download');
$id_file = $this->uri->segment(3);
$path = base_url()."images/tmp";
$this->load->database();
$show = "select * from tb_file where id_file = ".$id_file.";
$row = $this->db->query($show)->row();
$image = $row->content;
$name = $row->name;
$type = $row->type;
$size = $row->size;
$data = file_put_contents($path."/".$name, base64_decode($image));
header($type);
force_download($name, $data);
}
我的代码中缺少什么?它是下载但没有大小
注意:使用codeigniter框架
我认为在下载之前你不需要再将blob保存到文件中。不使用codeigniter下载帮助程序尝试此操作:
public function download(){
$id_file = $this->uri->segment(3);
$this->load->database();
$show = "select * from chat,tb_user,tb_file where chat.id_user = tb_user.id_user and chat.id_file = tb_file.id_file and tb_file.id_file = ".$id_file." order by waktu desc ";
$row = $this->db->query($show)->row();
$image = $row->content;
$name = $row->name;
$type = $row->type;
$size = $row->size;
header('Content-length: ' . $size);
header('Content-type: ' . $type);
header('Content-Disposition: attachment; filename=' . $name);
ob_clean();
flush();
echo hex2bin($image); // or base64 decode
exit;
}