1

我试图拒绝访问我服务器上的一个文件夹,其中有些文件只能通过 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 时,一切都很好。我不知道错误在哪里。谢谢你的帮助

4

1 回答 1

0

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

例如 /home/person/file.exe

于 2012-06-04T22:07:39.913 回答