【问题标题】:Providing downlaod link to file in non-public section of server在服务器的非公开部分提供文件的下载链接
【发布时间】:2012-12-14 06:53:52
【问题描述】:

我有一些文件存储在我网站的可公开访问部分之外的文件夹中。

例如,文件'abcd.jpg'(就服务器而言)存储在'/home/private_files/',网站在'/home/public_html/website.com/'

当我去提供查看文件的链接时,我使用

<a href="/home/private_files/abcd.jpg">Download</a> 但是这不起作用。

有什么建议吗?

【问题讨论】:

    标签: php html download directory private


    【解决方案1】:

    如果你使用 Apache 作为服务器,你可以将它设置为 httpd.conf 中的一个目录的别名...

    Alias /images/ "/home/private_files/"
    
    <Directory "/home/private_files/">
        Options Indexes MultiViews
        AllowOverride None
        Order allow,deny
        Allow from all
    </Directory>
    
    </IfModule>
    

    你可以用这个

    <a href="/images/abcd.jpg">Download</a>
    

    【讨论】:

    • 我会试试这个,谢谢!但是如果我去www.mysite.com/images/abcd.jpg 我能看到文件吗?因为我将它们放在非公共文件夹中的全部原因是为了防止这种情况发生?
    • 你可以把这个 Allow from [Your ip ADDRESS] 替换到目录结构的最后一行,只允许访问你的 ip 地址,希望这对你有用
    【解决方案2】:

    您无法访问位于 www 之外的目录。

    使用symlink 链接到文件:

    ln -s /home/private_files/abcd.jpg /home/www/abcd.jpg
    

    并在截止日期后删除链接(例如 rapidshare)

    或使用像this这样的文件下载器

    【讨论】:

    • 我也会试一试,但你能建议我如何在 PHP 中做到这一点吗?
    • 我想你忘了看我链接的课程。您应该使用此 bold readfile('/home/private_files/abcd.jpg'); 读取文件并使用标题向客户端显示下载窗口,例如 bold header('Content-Type: application/octet-stream');header("Content-Transfer-Encoding: Binary"); header("Content-disposition: attachment;filename=\"new_file_name\"");
    猜你喜欢
    • 2011-08-09
    • 2013-08-22
    • 1970-01-01
    • 2023-03-28
    • 2011-12-05
    • 1970-01-01
    • 2012-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多