2012-06-04 24 views
1

我想拒绝访问我的服务器上的文件夹,其中有文件只能通过php脚本下载。 PHP脚本来迫使文件的下载,就是这样:强制下载和htaccess =零字节文件

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的只能到本地主机提供接入:

order deny,allow 
deny from all 
allow from 127.0.0.1 

但每一次的脚本下载一个零个字节具有相同名称的文件,而不是完整的文件。然而,当我删除htaccess时,一切都很好..我无法弄清楚错误在哪里。 感谢您的帮助

+0

readfile()试图读取的文件和目录的权限是什么?请记住,Web服务器必须有权访问它。因此,如果您的Web服务器以用户httpd身份运行,则httpd必须具有对该文件的读取权限。你可以通过运行sudo -u httpd cat/path/to/file来测试。 –

+0

谢谢@jedwards,将$ file设置为绝对路径解决了问题! –

回答

0

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

例如/home/person/file.exe