在readfile上包含源文件名?

时间:2013-04-06 14:30:01

标签: php path download readfile

我有一个非常简单的hide-download-path-script设置,如下所示:

在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

有没有办法不仅可以更改文件名,还可以更改扩展名?

2 个答案:

答案 0 :(得分:2)

我遇到了同样的问题

解决方法如下:

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


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


readfile($filename);

我希望它能帮助你

答案 1 :(得分:0)

你的链接去保存file.php,浏览器永远不会有另一个文件名,然后保存file.php。 您需要添加如下标题:

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

或更好......

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

希望它有所帮助!