实现原理:我们仅仅只需要修改页面http头,把content-type设置为force-download,问题即可解决。
请看代码:
复制代码 代码如下:
forcedownload("pdfdemo.pdf");
function forcedownload($filename) {
if (false == file_exists($filename)) {
return false;
}
// http headers
header('content-type: application-x/force-download');
header('content-disposition: attachment; filename="' . basename($filename) .'"');
header('content-length: ' . filesize($filename));
// for ie6
if (false === strpos($_server['http_user_agent'], 'msie 6')) {
header('cache-control: no-cache, must-revalidate');
}
header('pragma: no-cache');
// read file content and output
return readfile($filename);;
}
为了方便,我写了一个函数forcedownload(),然后通过调用该函数即可。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!