2017-12-20 45 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를 저장 할 필요가 없습니다 생각합니다. 된답니다 다운로드 도우미를 사용하지 않고이 시도 :

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