我如何创建一个 PHP 脚本/页面,允许会员/买家下载存储在根目录之外的下载文件夹中的压缩文件(产品)?我正在使用 Apache 服务器。请帮忙!
谢谢!
保罗·G.
您可能会在 @soac 提供的链接中找到一些更好的信息,但以下是我的一些仅适用于 PDF 文件的代码的摘录:
<?php
$file = ( !empty($_POST['file']) ? basename(trim($_POST['file'])) : '' );
$full_path = '/dir1/dir2/dir3/'.$file; // 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
{
header("HTTP/1.1 404 Not Found");
exit;
}
?>
请注意,这将在浏览器中打开 PDF,而不是直接下载 PDF,尽管您可以从阅读器中将文件保存在本地。使用readfile()
可能比我在本例中通过句柄打开文件的旧方法更有效(并且代码更清晰)。
readfile($full_path);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)