我有很多文件存储在服务器上,但不在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;
}
如果它在public_html文件夹之外,则从该文件创建一个php脚本来file_get_contents。
这是我使用的脚本的一部分
if ($type == "1" && $method == "f"){
header('Content-type: application/pdf');
} else {
header('Content-type: image/jpeg');
}
$fp = fopen($uploaded_dir . $result['theimage'], "r");
while ($data = fread($fp, 1024)){
echo $data;
}
fclose($fp);