包括readfile上的源文件名


Including source file name on readfile?

我有一个非常简单的隐藏下载路径脚本设置,像这样:

在index.html中我有这个链接:

<a href="savefile.php">save</a>

在savefile.php中有这样一段代码:

$file = 'http://www.mysite.com/files/correct_horse_battery_staple.rar';
header("Content-Type: application/force-download");
@readfile($file);

这似乎可以工作,但不幸的是它将文件下载为savefile.php而不是correct_horse_battery_staple.rar

是否有办法不仅改变文件名,而且扩展名?

我也遇到过同样的问题

解如下:

header("Content-Type: application/force-download");

header('Content-Disposition: attachment; filename="'.$filename.'"');

readfile($filename);

我希望它对你有帮助

您的链接到savefile.php,并且浏览器永远不会得到除savefile.php以外的其他文件名。您需要添加一个标题,如:

header('Content-Disposition: attachment; filename="correct_horse_battery_staple.rar"');

或更好的…

header('Content-Disposition: attachment; filename="'.basename($file).'"');

希望有帮助!