【发布时间】:2019-05-06 04:31:40
【问题描述】:
背景:
我正在尝试将 PDF 文件提供给我的用户,但不能提供给其他人。建议似乎是将这些放在站点根目录之上但网络根目录之下。第一步是测试文件是否存在,结果始终为 FALSE。 问题:为什么 file_exists 总是返回 false? 尝试过的事情 1. 在 Web 根目录中创建名为“a”的目录(以避免拼写错误)。 file_exists("/a/") 为假 file_exists("/a/info.pdf") 为假,尽管文件存在 2. clearstatcache();在 file_exists 之前 3. 添加allow_url_fopen = on 到根目录下的php.ini
enter code here
clearstatcache();
$full_path = '/a/info.pdf'; // absolute physical path to file below web root.
if ( file_exists($full_path) )
{
$mimetype = 'application/pdf';
header('Cache-Control: no-cache');
header('Cache-Control: no-store');
header('Pragma: no-cache');
header('Content-Type: ' . $mimetype);
header('Content-Length: ' . filesize($full_path));
$fh = fopen($full_path,"rb");
while (!feof($fh)) { print(fread($fh, filesize($full_path))); }
fclose($fh);
}
else die("File does not exist on the server - .");
总是跟在 else 后面。 我还能尝试什么?
【问题讨论】:
-
file_exists()使用文件系统的绝对文件路径而不是 Web 根目录。