php下载文件的代码示例
更新时间:2012年06月29日 14:32:13 作者:
php下载文件的代码示例,需要的朋友可以参考下
复制代码 代码如下:
<?php
$file = 'monkey.gif';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
?>
以上代码是下载代码
接下来贴一段在线预览pdf文件的代码
复制代码 代码如下:
<?php
public function fddAction()
{
// get attachment location
$attachment_location = $_SERVER["DOCUMENT_ROOT"] . "/pdf/fdd/sample.pdf";
if (file_exists($attachment_location)) {
// attachment exists
// send open pdf dialog to user
header('Cache-Control: public'); // needed for i.e.
header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="sample.pdf"');
readfile($attachment_location);
die(); // stop execution of further script because we are only outputting the pdf
} else {
die('Error: File not found.');
}
}
?>
相关文章
Zend Framework教程之Zend_Config_Xml用法分析
这篇文章主要介绍了Zend Framework教程之Zend_Config_Xml用法,结合实例形式较为详细的分析了Zend_Config_Xml的功能,使用方法及相关注意事项,需要的朋友可以参考下2016-03-03thinkPHP+phpexcel实现excel报表输出功能示例
这篇文章主要介绍了thinkPHP+phpexcel实现excel报表输出功能,结合具体实例形式分析了thinkPHP整合PHPExcel实现针对Excel文件相关操作技巧,需要的朋友可以参考下2017-06-06
最新评论