您的位置:首页 > 编程语言 > PHP开发

php强制下载文件函数

2013-05-29 16:25 225 查看
有些类型文件(如txt,dat等),浏览器打开是默认是直接打开内容,也许你使用了<a href="url"></a>,但是浏览器才不管你那一套,照样直接打开。

我想实现直接下载怎么办:一下是php下载文件函数:

function downloadFile($file){

/*Coded by Alessio Delmonti*/

$file_name = $file;

$mime = 'application/force-download';

header('Pragma: public'); // required

header('Expires: 0'); // no cache

header('Cache-Control: must-revalidate, post-check=0, pre-check=0');

header('Cache-Control: private',false);

header('Content-Type: '.$mime);

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

header('Content-Transfer-Encoding: binary');

header('Connection: close');

readfile($file_name); // push it out

exit();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: