使用PHP强制下载PDF文件

原创文章 作者:月光光 2011年01月14日 10:19helloweba.com 标签:PHP 

我们有时会遇到这样一种情况,当需要下载一个PDF文件时,如果不经处理会直接在浏览器里打开PDF文件,然后再需要通过另存为才能保存下载文件。本文将通过PHP来实现直接下载PDF文件。

实现原理:我们仅仅只需要修改页面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(),然后通过调用该函数即可。请点击这里看演示DEMO。

声明:本文为原创文章,helloweba.net和作者拥有版权,如需转载,请注明来源于helloweba.net并保留原文链接:https://www.helloweba.net/php/103.html

1条评论

  • 直接用load标签