【问题标题】:Control access to files available for download控制对可供下载的文件的访问
【发布时间】:2010-10-04 18:23:28
【问题描述】:

我有一个文件夹,其中包含我的 ZF 应用程序可以向已登录用户发送的上传文档。我希望他们能够使用http://server/documents/filename.pdf 之类的链接并下载文件,但我希望有一个控制器DocumentsController 使现有用户cookie 能够验证他们是否已登录并有权下载文件。如果不需要,我不想使用像 http://server/documents/index/id/1 这样的 URL,尽管这不是一个糟糕的选择。

【问题讨论】:

    标签: php zend-framework .htaccess zend-controller


    【解决方案1】:

    您可以使用 X-SendFile 来获得最佳性能。它由 Apache (mod_xsendfile)、Lighttpd 和 Nginx 支持。该请求首先由一个 php 进程处理,该进程放置一个特殊的标头(X-Sendfile 或 Nginx 的 X-Accel-Redirect),当脚本结束时,Web 服务器接管并像静态文件一样发送文件。它速度更快,使用更少的内存。

    要将所有请求重定向到您的控制器,您需要在引导程序中编写自定义路由:

    protected function _initRouter()
    {
        $router = Zend_Controller_Front::getInstance()->getRouter();
    
        $documentRoute = new Zend_Controller_Router_Route(
            'document/:filename',
            array(
                'action'     => 'xsendfile',
                'controller' => 'documents'
            ),
            array(
                'filename' => '\..+$'
            )
        );
        $router->addRoute('document', $documentRoute );
    
        return $router;
    }
    

    您可以使用此操作助手来处理 x-sendfile 标头:http://www.zfsnippets.com/snippets/view/id/27,并且您需要有代码来检查用户是否已通过身份验证。

    【讨论】:

    • 这听起来很棒。我会试一试,让你知道!
    【解决方案2】:

    您必须使用Zend_Acl 来控制对DocumentsController 的访问,然后创建custom route 以将http://server/documents/* 重定向到http://server/documents/index/id/*

    编辑:

    solution proposed by Tomáš 会更好地处理更大的文件。

    【讨论】:

      猜你喜欢
      • 2013-09-29
      • 2019-02-09
      • 2015-01-16
      • 1970-01-01
      • 1970-01-01
      • 2015-10-31
      • 2012-12-25
      • 2015-03-25
      • 2011-04-26
      相关资源
      最近更新 更多