php header 设置文件下载

控制器 php 代码:
$filepath = "http://www.jiesen365.com/uploadfile/20150519044903-897.pdf";   //下载文件地址
$arr = explode( '/' , $filepath);
$filename= $arr[count($arr)-1]; //设置下载文件名称,注意后缀名
if($filepath){
header("Content-type: text/plain");
header("Accept-Ranges: bytes");
header("Content-Disposition: attachment; filename=".$filename);
header("Cache-Control: must-revalidate, post-check=0, pre-check=0" );
header("Pragma: no-cache" );
header("Expires: 0" );
readfile($filepath);
exit(0);
}
前台html代码:
<a href="javascript:;" onclick="download()">点击下载</a>
function download(){
window.location.href="连接--访问控制器(自动下载)"
}