阻止在CI中直接访问下载的文件

时间:2009-12-22 01:49:56

标签: codeigniter

如何使用CodeIgniter阻止直接访问我的下载文件。

假设我将所有文件存储在

应用/ secretdirectory / file1.jpg 应用/ secretdirectory / file2.text 应用/ secretdirectory / file3.zip

通常,我创建直接链接来访问这些文件。

<a href="application/secretdirectory/file3.zip" > download here </a>

我想阻止它,我希望我的链接将是

<a href="<? echo site_url() . "download/file3.zip"  ?>" > download here </a>
你可以帮助我吗?

2 个答案:

答案 0 :(得分:4)

将它传递给这样的控制器可能是一个好主意:

Class File extends Controller{

    function download($file){
        $data = file_get_contents('path/to/' . $file); // Read the file's contents
        $name = $file;
        force_download($name, $data);
    }
}

您的链接将是:

<?php echo anchor('file/download/' . $thefile, 'Download');?>

这样他们就永远知道它来自哪个目录。

答案 1 :(得分:0)

我确信CI中有一个库可以帮助解决这个问题。

基本上你发送了正确MIME类型的标题,内容长度(filesize()),然后输出到浏览器下载echo file_get_contents()可能有效)

如果使用Apache,您还要拒绝显示directoy内容。

<Files .*>
    Order Deny,Allow
    Deny From All
</Files>
相关问题