2017-12-20 22 views
0

blob型のデータベースからファイルをダウンロードする方法。私はすでにイメージから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のフレームワーク

+0

あなたはより良いあなたが私たちがありがとう – RiggsFolly

+0

2 processessを比較することができます最初の場所でのデータベース上にファイルを保存する方法私たちに示していました。私はすでに画像を保存しています – Stfvns

+2

Errrrイメージを保存するときには 'bin2hex()'を使い、 'base64_decode()'を読み込んだときはそれを使用しますか?彼らは全く同じものではありません – RiggsFolly

答えて

1

を使用して、私はあなたがダウンロードする前に、もう一度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; 
} 
+0

もう一度チェックしてください! – RiggsFolly

関連する問題