允许用户下载不在 Web 目录中的文件


Let users download files that are not in a web directory?

可能的重复项:
允许用户在网页根目录之外下载文件

嗨,伙计们,我想使用 php 允许用户从我的服务器上不属于我的"网络"文件的目录中下载文件......有没有办法做到这一点?

例如

我的网络文件位于/www/bla/bla

但是我想下载的文件在/home/bla/bla 中

你可以做的是这样的:

$file = '/home/bla/bla/file.ext';    
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;

readfile() 只是检索文件的内容并打印出来。其余代码是强制下载。