2011-01-24 89 views
1

我在zend框架中開發應用程序。我想在公共文件夾之外存儲某些安全圖像和pdf文檔,例如/ project/data/uploads或/ projects/application/data/uploads。在zend框架中訪問公共文件夾外的圖像或文檔

我無法訪問公用文件夾以外的圖像/ pdf文檔。 有人可以提出一種方法來做到這一點。

謝謝

+0

您是否正確設置了上傳文件夾的權限? – Marcin 2011-01-24 11:20:28

回答

2

你必須有一個知道如何獲取並提供所有的東西單獨行動。類似這樣的:

public function viewpdfAction() 
{ 
    $id = (int) $this->_getParam('id', 0); 

    // You implement some function - either here in your controller 
    // or someplace else - to get the pdf name from the passed id. 
    // Alternatively, you can pass the name itself. Up to you, of course. 
    // The key is that the request somehow identifies the file requested. 
    $pdfName = $this->_mapPdfIdToName($id); 

    // The path to the real pdf 
    $pdfFile = '/project/data/uploads/pdf/' . $pdfName; 

    // Disable rendering 
    $this->_helper->viewRenderer->setNoRender(true); 

    // Send the right mime headers for the content type 
    $this->getResponse() 
     ->setBody('') 
     ->setHeader('Cache-control', 'public') // needed for IE, I have read 
     ->setHeader('Content-type', 'application/pdf') 
     ->setHeader('Content-Disposition', sprintf('attachment; filename="%s"', $pdfName)); 

    // Send the content 
    readfile($pdfFile); 
} 

當然,其中一些可以推入服務類,以儘可能減少控制器。在這方面每個人都有不同的口味。

我承認這段代碼沒有完全測試,主要是試圖給出基本的想法。如果我在這裏做了一個總的骨頭錯誤,請讓我知道。

希望它有幫助!

相關問題