【问题标题】:Force download and htaccess = zero bytes files强制下载和 htaccess = 零字节文件
【发布时间】:2012-06-08 21:33:24
【问题描述】:

我试图拒绝访问我服务器上的一个文件夹,其中有些文件只能通过 php 脚本下载。 php脚本用于强制下载文件,就像这样:

  function downloadFile($file){
        $file_name = $file;
        $mime = 'application/force-download';
        header('Pragma: public');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Cache-Control: private',false);
        header('Content-Type: '.$mime);
        header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
        header('Content-Transfer-Encoding: binary');
        header('Content-Length: '.filesize($file_name));    // provide file size
        header('Connection: close');
        readfile($file_name);
        exit();
    }

并且 htaccess 必须只提供对 localhost 的访问:

order deny,allow
deny from all
allow from 127.0.0.1

但是每次脚本下载一个零字节的同名文件,而不是完整的文件。但是,当我删除 htaccess 时,一切都很好。我不知道错误在哪里。 感谢您的帮助

【问题讨论】:

  • readfile() 试图读取的文件和目录的权限是什么?请记住,Web 服务器必须有权访问它。因此,例如,如果您的 Web 服务器以用户 httpd 的身份运行,那么 httpd 必须具有对该文件的读取权限。你可以通过运行类似 sudo -u httpd cat /path/to/file 的东西来测试它。
  • 感谢@jedwards,将 $file 设置为绝对路径已解决问题!

标签: php .htaccess


【解决方案1】:

问题在于 readfile();需要机器上文件的绝对路径而不是url。

例如/home/person/file.exe

【讨论】:

    猜你喜欢
    • 2016-01-16
    • 2014-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-28
    • 2011-05-12
    • 2015-05-08
    • 1970-01-01
    相关资源
    最近更新 更多