开发者

Access images or doc outside public folder in zend framework

I developing an application in zend framework. I want to store certain secure images and pdf documents outside public folder like /project/data/uploads or /projects/application/data/uploads.

I am not开发者_Python百科 able to access the images/pdf documents other than public folder. Can someone suggest a way to do it.

thank you


You have to have a separate action that knows how to fetch and deliver all that stuff. Something like this:

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);
}

Of course, some of this can be pushed down into service classes to keep the controller as thin as possible. Everyone has different tastes in this regard.

I confess that this code not completely tested, mostly trying to give the basic idea. If I have made a total bonehead error in here, please let me know.

Hope it helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜