【发布时间】:2012-11-11 01:11:03
【问题描述】:
我正在使用 PHP 作为文件下载页面。这是问题代码的sn-p:
if (file_exists($attachment_location)) {
header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
header("Cache-Control: public"); // needed for i.e.
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: Binary");
header("Content-Length:".filesize($attachment_location));
header("Content-Disposition: attachment; filename=file.zip");
readfile($attachment_location);
die("Hooray");
}
else {
die("Error: File not found.");
}
此代码在本地测试时运行良好,但在部署到实时服务器后,浏览器返回“找不到页面”错误。我认为这可能是一个 .htaccess 问题,但实时服务器上的所有 .htaccess 文件都与它们的本地对应文件相同。我的下一个猜测是实时服务器的 PHP 配置,但我不知道什么 PHP 设置可能会导致这种行为。
file_exists() 函数总是返回 true - 我已经在实时服务器上检查了这一点,它总是正确地获取文件、文件的大小等,所以它确实有文件的句柄。它只是不会执行下载!
主站点是 Wordpress 站点,但此代码不是 Wordpress 页面的一部分 - 它位于站点根目录中的独立目录中。
更新:is_file() 和 is_readable() 都为文件返回 true,所以这不是问题。导致问题的具体行是:
readfile($attachment_location)
在那之前的一切都非常快乐。
【问题讨论】:
-
尝试回显文件路径,看看是否有什么可疑之处
-
也许在您的 Live Server 中您已经配置了您的 apache 站点可用配置,拒绝用户访问。 PHP 可能能看到文件,但不能看到用户。
-
如果 file_exists() 始终返回 true,则“else”代码路径无法执行。
-
文件路径看起来不错。我将查看 apache 中可用的站点配置。并且 file_exists 总是返回 true 不是问题 - 我不希望'else'代码路径无论如何执行!我只是想指出问题似乎不在于文件被拾取。
-
我猜额外的下划线只是一个错字?
标签: php wordpress .htaccess download