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="连接--访问控制器(自动下载)"
}




posted @   微客鸟窝  阅读(1769)  评论(0编辑  收藏  举报
努力加载评论中...
/* 看板娘 */
点击右上角即可分享
微信分享提示