标签:php
我有许多存储在服务器上的文件,但没有存储在public_html目录中.这个想法是,登录的用户可以使用$_SESSION变量来检查文件是否已登录,但可以下载文件,但是如果其他人使用他们的计算机,他们将无法在浏览器历史记录中看到直接文件路径,即使他们它在公共html目录之外,因此无法访问.
我知道我需要一个脚本来执行此操作,但是我无法在任何地方找到操作方法,如果有人可以告诉我如何执行操作,将不胜感激.
解决方法:
您可以使用readfile获取输出作为有问题的文件.例如:
$file = '/absolute/path/to/file.ext';
if (file_exists($file)) {
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, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
标签:php
来源: https://codeday.me/bug/20191011/1889783.html