代码之家  ›  专栏  ›  技术社区  ›  Adi Sembiring

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

  •  1
  • Adi Sembiring  · 技术社区  · 15 年前

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

    我应该把我所有的文件都存储在

    应用程序/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 回复  |  直到 15 年前
        1
  •  4
  •   Teej    15 年前

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

    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');?>
    

    这样他们就会 从未 知道它来自哪个目录。

        2
  •  0
  •   alex    15 年前

    我相信在CI中有一个图书馆可以帮助解决这个问题。

    基本上,您发送正确的mime类型的头,内容长度( filesize() )然后输出到浏览器下载echo file_get_contents() 可以工作)

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

    <Files .*>
        Order Deny,Allow
        Deny From All
    </Files>