Control access to files available for download

痴心易碎 提交于 2019-12-01 01:21:18

You can use X-SendFile to obtain the best performance. It is supported by Apache (mod_xsendfile), Lighttpd and Nginx. The request is first handled by a php process which put a special header (X-Sendfile or X-Accel-Redirect for Nginx) and when the script end, the web server take over and send the file like a static file. It is faster and use less memory.

To redirect all the request to your controller, you need to write a custom route in your bootstrap :

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

You can use this action helper to handle the x-sendfile header : http://www.zfsnippets.com/snippets/view/id/27 and you need to had code to check if the user is authenticated.

You'll have to use Zend_Acl to control access to the DocumentsController and then create custom route to redirect http://server/documents/* to http://server/documents/index/id/*.

Edit:

The solution proposed by Tomáš will work better for bigger files.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!