2013-04-06 58 views
1

我有一个非常简单的隐藏下载路径脚本的设置是这样的:在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

有什么办法可以改变文件名,也可以改变扩展名吗?

+3

尝试['header('Content-Disposition:attachment; filename =“correct_horse_battery_staple.rar'');'](http://us.php.net/manual/en/function.header.php#example- 4365)。如果你的文件名包含非ASCII字符,那么让它在浏览器中显示会更加复杂。 (编辑:另外,使用本地路径而不是基于'http://'的文件。) – DCoder 2013-04-06 14:32:59

回答

2

我有同样的问题

解决如下:

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


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


readfile($filename); 

我希望它能帮助ü

+0

啊谢谢! (我什至不能upvote你,sry。) – 2013-04-06 14:37:57

+0

好吧!没问题 :) – Heberfa 2013-04-06 14:48:35

0

您的链接去savefile.php,浏览器从来没有得到另一名比SAVEFILE .PHP。 您需要添加一个头,如:

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

或更好...

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

希望它能帮助!

相关问题