【问题标题】:Access images or doc outside public folder in zend framework在zend框架中访问公共文件夹外的图像或文档
【发布时间】:2011-01-24 11:16:27
【问题描述】:

我在 zend 框架中开发一个应用程序。我想将某些安全图像和 pdf 文档存储在 /project/data/uploads 或 /projects/application/data/uploads 等公共文件夹之外。

我无法访问公用文件夹以外的图像/pdf 文档。 有人可以建议一种方法吗?

谢谢

【问题讨论】:

  • 您是否正确设置了上传文件夹的权限?

标签: zend-framework


【解决方案1】:

您必须有一个单独的操作来知道如何获取和交付所有这些东西。像这样的:

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

当然,其中一些可以下推到服务类中,以使控制器尽可能精简。在这方面每个人都有不同的品味。

我承认这段代码没有经过完全测试,主要是为了给出基本的想法。如果我在这里犯了一个彻头彻尾的错误,请告诉我。

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 2011-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-27
    • 2012-03-28
    • 2022-11-24
    相关资源
    最近更新 更多