php下载文件的代码示例
程序员文章站
2023-02-26 13:37:25
复制代码 代码如下:
<?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.');
}
}
?>
复制代码 代码如下:
<?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.');
}
}
?>