作者 邓超

附件预览

... ... @@ -23,16 +23,18 @@ class Attachment extends Base {
public function show(){
$path = app()->request('path');
$filename = ROOT_PATH.$path;
$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'])){
header("Content-type: ".$file->getMTime());
header("Content-Disposition: inline; filename=".$file->getFilename());
header("Content-Length: ".$file->getSize());
header("Cache-Control: max-age=0");
header("Last-Modified: ".$file->getMTime());
header("Expires: " . gmdate("D, d M Y H:i:s", time() + 3600 * 24 * 365) . " GMT");
app()->header("Content-Type",$file->getMTime());
app()->header("Content-Disposition","inline; filename=".$file->getFilename());
app()->header("Content-Transfer-Encoding","binary");
app()->header("Accept-Ranges","bytes");
app()->header("Content-Length",$file->getSize());
app()->header("Cache-Control","max-age=0");
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>";
... ...
... ... @@ -70,6 +70,11 @@ class App {
private $bodyRaw = '';
/**
* @var array
*/
protected $headers = [];
/**
* App constructor.
*/
public function __construct()
... ... @@ -360,10 +365,12 @@ class App {
/**
* @param $data
* @param int $http_code
* @param array $headers
* @return mixed
* @author:dc
* @time 2023/3/27 10:53
* @time 2025/8/20 9:53
*/
public static function echo($data, $http_code = 200){
public static function echo($data, $http_code = 200,array $headers = []){
if(php_sapi_name()=='cli'){
... ... @@ -374,11 +381,18 @@ class App {
if(is_array($data)){
@header("Content-Type:application/json; charset=UTF-8");
echo json_encode($data,JSON_UNESCAPED_UNICODE);
$data = json_encode($data,JSON_UNESCAPED_UNICODE);
}else{
@header("Content-Type:text/html; charset=UTF-8");
echo $data;
}
if($headers){
foreach ($headers as $name=>$value){
@header($name.':'.$value);
}
}
echo $data;
}
public function getError(){
... ... @@ -386,6 +400,20 @@ class App {
}
/**
* 输出头信息
* @param $name
* @param $value
* @author:dc
* @time 2025/8/20 9:50
*/
public function header($name,$value){
$name = str_replace('-',' ',$name);
$name = ucwords($name);
$name = str_replace(' ','-',$name);
$this->headers[$name] = $value;
}
/**
* 结束
* @author:dc
* @time 2023/2/13 10:54
... ... @@ -408,7 +436,7 @@ class App {
$data['debug'] = $last_error;
}
self::echo($data,502);
self::echo($data,502,$app->headers);
return true;
}
... ... @@ -420,7 +448,7 @@ class App {
logs($app->getError());
}
self::echo($app->data);
self::echo($app->data,200,$app->headers);
// 日志记录
... ...