首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Kohana 3.1中设置受保护的静态文件

问如何在Kohana 3.1中设置受保护的静态文件
EN

Stack Overflow用户
提问于 2011-06-08 10:28:33
回答 1查看 1K关注 0票数 1

我使用的是启用了ORM/Auth模块的Kohana 3.1。我想提供静态文件(文档、pdf等)仅限于A_only目录中角色为A的用户。

由于.htaccess文件只提供它直接找到的URL,而不是传递给index.php,所以我可以通过.htaccess拒绝A_only中的所有访问,但是我如何在控制器函数中提供静态文件呢?

我还可以在A_only目录中有一个需要身份验证的.htaccess。但是,这将要求他们重新登录,即使我将其设置为在数据库中查找用户/密码。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-06-08 17:58:11

你需要告诉你的web服务器停止处理静态文件。最简单的解决方案是将静态文件移到web目录之外,这样Apache就无法找到它们;这将强制该请求通过Kohana。

第二部分是创建一个控制器,它为您处理权限和文件发送。Kohana用户指南有一个相当好的例子,可以帮助你完成以下工作:

代码语言:javascript
复制
// Get the file path from the request
$file = $this->request->param('file');

// Find the file extension
$ext = pathinfo($file, PATHINFO_EXTENSION);

// Remove the extension from the filename
$file = substr($file, 0, -(strlen($ext) + 1));

if ($file = Kohana::find_file('media/guide', $file, $ext))
{
    // Check if the browser sent an "if-none-match: <etag>" header, and tell if the file hasn't changed
    $this->response->check_cache(sha1($this->request->uri()).filemtime($file), $this->request);

    // Send the file content as the response
    $this->response->body(file_get_contents($file));

    // Set the proper headers to allow caching
    $this->response->headers('content-type',  File::mime_by_ext($ext));
    $this->response->headers('last-modified', date('r', filemtime($file)));
}
else
{
    // Return a 404 status
    $this->response->status(404);
}

您需要担心的主要问题是更改Kohana查找文件的位置:

代码语言:javascript
复制
if ($file = Kohana::find_file('media/guide', $file, $ext))

剩下的都是样板文件。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6273587

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档