2010-06-03 4 views
1

서버에 저장된 암호화 된 파일을 열 수있는 방법을 찾고 있습니다. mcrypt를 사용하여 파일을 암호화하고 있습니다.암호화 된 파일 열기

처음에는 파일을 열고 해독 한 다음 새 위치에 쓰고 그 파일을 열 수있는 클래스를 만들려고했습니다. 그러나 나는 더 나은 방법이 있다는 것을 스스로 확신했습니다 (나는 그것이 무엇인지 모릅니다). 일단 해독되면 브라우저에 스트리밍 할 수있는 방법이있는 것 같습니다.

초기 설정은 파일 위치로 연결되며 브라우저가 대신 사용됩니다 (예 : .pdf 파일은 파일을 열거 나 저장하기위한 대화 상자를 표시합니다). 가능하다면 해독 한 후에도 동일한 작업을 수행하고 싶습니다.

포인터? 조언? 뷰어?

감사합니다.


편집 : 지금 무엇을 노력하고 있습니다

이된다

function decryptFile($path){ 
    $folder=BASE_PATH.$path.'/'.$this->uri->segment(4); 
    if(!file_exists($folder.'/tmp')){ 
     mkdir($folder.'/tmp', 0700); 
     chmod($folder.'/tmp', 0700); 
    } 
    $tmpfn=BASE_PATH.$path.'/'.$this->uri->segment(4).'/tmp/'.$this->uri->segment(5); 
    $p=BASE_PATH.$path.'/'.$this->uri->segment(4).'/'.$this->uri->segment(5); 
    $pc=file_get_contents($p) or die ('no gfc'); 
    $pcue=$this->encrypt->decode($pc,$this->e_key) or die ('no decode'); 
    $pp=fopen($tmpfn,"w") or die ('no fopen'.$tmpfn); 
    fwrite($pp,$pcue) or die ('no write'); 
    fclose($pp); 

    if (file_exists($tmpfn)) { 
     header('Content-Description: File Transfer'); 
     header('Content-Type: application/octet-stream'); 
     header('Content-Disposition: attachment; filename='.basename($tmpfn)); 
     header('Content-Transfer-Encoding: binary'); 
     header('Expires: 0'); 
     header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
     header('Pragma: public'); 
     header('Content-Length: ' . filesize($tmpfn)); 
     ob_clean(); 
     flush(); 
     readfile($tmpfn); 
     unlink($tmpfn); 
     exit; 
    } 
} 

그러나이 방법을 (내 생각)하고 또 다른 부작용 것은 이제 대신 오픈을 받고 또는 대화를 저장하는 유일한 방법은 파일을 저장하는 것입니다. 가능하다면 나는 옵션을 갖고 싶어.


편집 - 최종 코드 :

function decryptFile(){ 
    extract($_POST); 
    $folder = $this->uri->segment(3); 
    $clientFolder = $this->uri->segment(4); 
    $fileName = $this->uri->segment(5); 
    $filePath=BASE_PATH.$folder.'/'.$clientFolder.'/'.$fileName; 
    $fileContents=file_get_contents($filePath) or die ('Could not get file contents.'); 
    $ue_fileContents=$this->encrypt->decode($pc,$this->e_key) or die ('Could not decode.'); 
    header('Content-Description: File Transfer'); 
    header('Content-Type: '.$type); 
    header('Content-Transfer-Encoding: binary'); 
    header('Expires: 0'); 
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
    header('Pragma: public'); 
    header('Content-Length: ' . strlen($ue_fileContents)); 
    echo $ue_fileContents; 
    exit; 
} 

답변

1

왜 임시 파일에 써야합니까? 다음과 같이 간단하게 할 수 있습니다.

function decryptFile($path){ 
    $folder=BASE_PATH.$path.'/'.$this->uri->segment(4); 
    $tmpfn=BASE_PATH.$path.'/'.$this->uri->segment(4).'/tmp/'.$this->uri->segment(5); 
    $p=BASE_PATH.$path.'/'.$this->uri->segment(4).'/'.$this->uri->segment(5); 
    $pc=file_get_contents($p) or die ('no gfc'); 
    $pcue=$this->encrypt->decode($pc,$this->e_key) or die ('no decde'); 
     header('Content-Description: File Transfer'); 
     header('Content-Type: application/octet-stream'); 
     header('Content-Disposition: attachment; filename='.basename($tmpfn)); 
     header('Content-Transfer-Encoding: binary'); 
     header('Expires: 0'); 
     header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
     header('Pragma: public'); 
     header('Content-Length: ' . strlen($pcue)); 
     echo $pcue; 
     exit; 

} 
1

그러나이 방법을 (내 생각) 일의 또 다른 부작용은 대신 열거 나 저장 대화를 얻는 지금, 유일한 옵션이 있다는 것이다 파일을 저장합니다. 차라리 파일의 실제 유형

header('Content-Type: application/octet-stream'); 

을 대체 할 수

하려고하면 옵션이있을 것이다.

header('Content-type: application/pdf'); 

또한 라인을 드롭 할 수 있습니다 :

header('Content-Disposition: attachment; filename='.basename($tmpfn)); 

는 그래서 대화 상자가 브라우저에 표시되지 않습니다 것을 예를 들어, 경우 그것은 PDF입니다.