0

我目前正在使用这个PHP 下载脚本从我的网站上提供一长串大文件(1GB+),但在抱怨下载损坏后,我研究并找到了一个更好看的替代方案:mod_xsendfile。我的主机是 Dreamhost,他们已经在我的域上启用了 xsendfile。我使用作者网站上的这段代码对其进行了测试,它可以工作:

<?php 
header("X-Sendfile: /home/username/website.com/test.zip");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=test.zip");
exit;
?>

但我想将所有下载内容放在一个目录中,并链接到文件,就像我使用媒体部门的脚本一样:

https://website.com/download.php?file=test.zip

我搜索了大多数用 x-sendfile 标记的问题,但没有发现任何有用的东西。我不会编写 PHP 代码,但知道足以配置脚本以使它们工作。有谁知道可以执行此操作或可以帮助我的脚本?

谢谢

4

1 回答 1

0

可以这样做:

<?php 
$path = realpath("/home/username/website.com/" . $_GET["file"]);
if (strpos($path, "/home/username/website.com/") !== 0) {
    header("Status: 404 Not Found");
    die();
}
header("X-Sendfile: $path");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=".$_GET["file"]);

这也确保用户无法从任何其他目录获取文件/home/username/website.com/,例如数据库备份或其他您不想下载的文件。

于 2015-01-18T23:10:48.720 回答