2013-09-25 2 views
3

현재 저는 zf2에서 일하고 있습니다. 지금은 PDF 파일을 다운로드하는 옵션을 다운로드해야합니다. 나는 data 디렉토리에있는 모든 PDF 파일을 저장했습니다. 어떻게 그 PDF 파일을 .phtml 파일에서 링크를 지정할 수 있습니까?젠드 프레임 워크의 데이터 폴더에있는 파일에 액세스하는 방법은 무엇입니까?

미리 감사드립니다.

+0

내 업데이트 된 답변을 참조하십시오. 이전 코드의 1 : 1 구현은 엄청난 보안 위험이 있습니다.) – Sam

답변

2

데이터 폴더에 PDF 파일의 공개 디렉토리에 symbolic link을 만듭니다. 예를 들어

: 사용자가 /data 디렉토리에 직접 액세스하지 않습니다

<a href="/pdf/file.pdf">File.pdf</a> 
7

처럼

ln -s /your/project/data/pdfdir /your/project/public/pdf 

만들고 링크 뭔가. 이것은 단지 그렇게 좋지 않을 것입니다. 그러나 당신은 쉽게 download-script.php 또는이 디렉토리의 내용을 사용자에게 넘겨 줄 수있는 자신을 쓸 수 있습니다. 당신이 public/index.php의 처음 여섯 개 라인을 살펴 경우

다음 볼 수 있습니다 : 염두에두고

<?php 
/** 
* This makes our life easier when dealing with paths. Everything is relative 
* to the application root now. 
*/ 
chdir(dirname(__DIR__)); 

, 당신이 알고있는 것들의 에서 PHP의 측면 제품 내부에 접근 data 디렉토리는 다음과 같이 간단합니다. data/file.pdf

사용자는 항상 일종의 다운로드 로거를 작성하고 싶습니다. 컨트롤러를 쓰십시오. 해당 컨트롤러 내부에서 조치를 취했거나 download 또는 이와 비슷한 것으로 처리했을 수 있습니다. 이 작업에는 하나의 매개 변수 filename이 있어야합니다.

이 작업은 파일 이름이 존재하는지 확인하는 것입니다 (file_exists('data/'.$filename)). 존재하는 경우이 파일을 사용자에게 전달하기 만하면됩니다. 예 혼합 또는 zf2 네이티브 PHP는 수 :

public function downloadAction() 
{ 
    $filename = str_replace('..', '', $this->params('filename')); 
    $file  = 'data/' . $filename; 

    if (false === file_exists($file)) { 
     return $this->redirect('routename-file-does-not-exist'); 
    } 

    $filetype = finfo_file($file); 
    header("Content-Type: {$filetype}"); 
    header("Content-Disposition: attachment; filename=\"{$filename}\""); 
    readfile($file); 

    // Do some DB or File-Increment on filename download counter 
    exit(); 
} 

이 깨끗 ZF2하지 않습니다하지만 난 지금 게으른입니다. 적절한 Response 개체를 사용하고 거기에 파일 처리를 수행하는 것이 훨씬 더 이상적 일 수 있습니다!

중요 업데이트이 항목은 실제로 매우 안전하지도 않습니다. 상위 폴더를 허용하지 않아야합니다. 당신은 싶어 ... 나는 단순히 충분해야 두 점의 모든 발행 수를 대체 완전히 틀리지 않는 경우이 사람이

`http://domain.com/download/../config/autoload/db.local.php` 

처럼 data/download 디렉토리 외부에 뭔가를 할

0

차입이없는 것 Sam의 코드는 ZF2 구문에서 다음과 같습니다.

public function downloadAction() 
{ 
    $filename = str_replace('..', '', $this->params('filename')); 
    $file  = 'data/' . $filename; 

    if (false === file_exists($file)) { 
     return $this->redirect('routename-file-does-not-exist'); 
    } 

    $filetype = finfo_file($file); 
    $response = new \Zend\Http\Response\Stream(); 
    $response->getHeaders()->addHeaders(array(
     'Content-Type' => $filetype, 
     'Content-Disposition' => "attachement; filename=\"$filename\"" 
    )); 
    $response->setStream(fopen($wpFilePath, 'r')); 
    return $response; 
} 
관련 문제