我正试图强制下载受保护的zip文件(我不希望人们在没有先登录的情况下访问它.
这是我的代码:
$file='../downloads/'.$filename;
header("Content-type: application/zip;\n");
header("Content-transfer-encoding: Binary");
header("Content-length: ".filesize($file).";\n");
header("Content-disposition: attachment; filename=\"".basename($file)."\"");
readfile("$file");
exit();
有任何想法吗?
解决方法:
此问题可能有多种原因.可能您的文件未找到或无法读取,因此文件的内容只是PHP错误消息.或者已经发送了HTTP标头.或者你有一些额外的输出,然后破坏你的文件的内容.
$file='../downloads/'.$filename;
if (headers_sent()) {
echo 'HTTP header already sent';
} else {
if (!is_file($file)) {
header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found');
echo 'File not found';
} else if (!is_readable($file)) {
header($_SERVER['SERVER_PROTOCOL'].' 403 Forbidden');
echo 'File not readable';
} else {
header($_SERVER['SERVER_PROTOCOL'].' 200 OK');
header("Content-Type: application/zip");
header("Content-transfer-encoding: Binary");
header("Content-Length: ".filesize($file));
header("Content-disposition: attachment; filename=\"".basename($file)."\"");
readfile($file);
exit;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。