欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php下载文件的代码示例

程序员文章站 2022-05-25 21:05:33
复制代码 代码如下:
复制代码 代码如下:

<?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.');
}
}
?>