作者 邓超

附件预览

... ... @@ -26,18 +26,28 @@ class Attachment extends Base {
$filename = PUBLIC_PATH.$path;
if(file_exists($filename)){
$file = new \SplFileInfo($filename);
if(in_array($file->getExtension(),['jpg','jpeg','png','gif','mp4','mp3','pdf','txt','doc','docx','xls','xlsx','ppt','pptx','eml'])){
app()->header("Content-Type",$file->getType());
app()->header("Content-Disposition","inline; filename=".$file->getFilename());
app()->header("Content-Transfer-Encoding","binary");
app()->header("Accept-Ranges","bytes");
// 'doc','docx','xls','xlsx','ppt','pptx'
if(in_array($file->getExtension(),[
'jpg','jpeg','png','gif','mp4','mp3','pdf','txt','eml','log','html','htm','htmls','xml','md'
])){
$mime = mime_content_type($file->getRealPath());
// 邮件类型
if (str_contains($mime, 'message/')){
$mime = "text/plain";
}else {
app()->header("Content-Disposition","inline; filename=".$file->getFilename());
app()->header("Content-Transfer-Encoding","binary");
app()->header("Accept-Ranges","bytes");
}
app()->header("Content-Type",$mime);
app()->header("Content-Length",$file->getSize());
app()->header("Cache-Control","max-age=0");
app()->header("Cache-Control","public, max-age=31536000");
app()->header("Last-Modified",$file->getMTime());
app()->header("Expires",gmdate("D, d M Y H:i:s", time() + 3600 * 24 * 365) . " GMT");
return file_get_contents($file->getPathname()) ;
}
return "<div style='text-align: center'>“".$file->getFilename()."“ 当前附件不支持预览</div>";
return "<div style='text-align: center'>当前附件不支持预览</div>";
}
return "<div style='text-align: center'>404 附件不存在</div>";
}
... ...