2011-01-24 53 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); 
} 

当然,其中一些可以推入服务类,以尽可能减少控制器。在这方面每个人都有不同的口味。

我承认这段代码没有完全测试,主要是试图给出基本的想法。如果我在这里做了一个总的骨头错误,请让我知道。

希望它有帮助!

相关问题