实现原理:我们仅仅只需要修改页面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(),然后通过调用该函数即可。
http://www.bkjia.com/phpjc/710592.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/710592.htmltecharticle我们有时会遇到这样一种情况,当需要下载一个pdf文件时,如果不经处理会直接在浏览器里打开pdf文件,然后再需要通过另存为才能保存下...
