首页 >后端开发 >php教程 >php下载文件的代码示例

php下载文件的代码示例

高洛峰
高洛峰原创
2016-12-23 12:46:111288浏览

<?php 
$file = &#39;monkey.gif&#39;; 

if (file_exists($file)) { 
header(&#39;Content-Description: File Transfer&#39;); 
header(&#39;Content-Type: application/octet-stream&#39;); 
header(&#39;Content-Disposition: attachment; filename=&#39;.basename($file)); 
header(&#39;Content-Transfer-Encoding: binary&#39;); 
header(&#39;Expires: 0&#39;); 
header(&#39;Cache-Control: must-revalidate, post-check=0, pre-check=0&#39;); 
header(&#39;Pragma: public&#39;); 
header(&#39;Content-Length: &#39; . 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(&#39;Cache-Control: public&#39;); // needed for i.e. 
header(&#39;Content-Type: application/pdf&#39;); 
header(&#39;Content-Disposition: inline; filename="sample.pdf"&#39;); 
readfile($attachment_location); 
die(); // stop execution of further script because we are only outputting the pdf 

} else { 
die(&#39;Error: File not found.&#39;); 
} 
} 
?>

更多php下载文件的代码示例相关文章请关注PHP中文网!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn