作者 邓超

附件预览

@@ -23,16 +23,18 @@ class Attachment extends Base { @@ -23,16 +23,18 @@ class Attachment extends Base {
23 23
24 public function show(){ 24 public function show(){
25 $path = app()->request('path'); 25 $path = app()->request('path');
26 - $filename = ROOT_PATH.$path; 26 + $filename = PUBLIC_PATH.$path;
27 if(file_exists($filename)){ 27 if(file_exists($filename)){
28 $file = new \SplFileInfo($filename); 28 $file = new \SplFileInfo($filename);
29 if(in_array($file->getExtension(),['jpg','jpeg','png','gif','mp4','mp3','pdf','txt','doc','docx','xls','xlsx','ppt','pptx','eml'])){ 29 if(in_array($file->getExtension(),['jpg','jpeg','png','gif','mp4','mp3','pdf','txt','doc','docx','xls','xlsx','ppt','pptx','eml'])){
30 - header("Content-type: ".$file->getMTime());  
31 - header("Content-Disposition: inline; filename=".$file->getFilename());  
32 - header("Content-Length: ".$file->getSize());  
33 - header("Cache-Control: max-age=0");  
34 - header("Last-Modified: ".$file->getMTime());  
35 - header("Expires: " . gmdate("D, d M Y H:i:s", time() + 3600 * 24 * 365) . " GMT"); 30 + app()->header("Content-Type",$file->getMTime());
  31 + app()->header("Content-Disposition","inline; filename=".$file->getFilename());
  32 + app()->header("Content-Transfer-Encoding","binary");
  33 + app()->header("Accept-Ranges","bytes");
  34 + app()->header("Content-Length",$file->getSize());
  35 + app()->header("Cache-Control","max-age=0");
  36 + app()->header("Last-Modified",$file->getMTime());
  37 + app()->header("Expires",gmdate("D, d M Y H:i:s", time() + 3600 * 24 * 365) . " GMT");
36 return file_get_contents($file->getPathname()) ; 38 return file_get_contents($file->getPathname()) ;
37 } 39 }
38 return "<div style='text-align: center'>“".$file->getFilename()."“ 当前附件不支持预览</div>"; 40 return "<div style='text-align: center'>“".$file->getFilename()."“ 当前附件不支持预览</div>";
@@ -70,6 +70,11 @@ class App { @@ -70,6 +70,11 @@ class App {
70 private $bodyRaw = ''; 70 private $bodyRaw = '';
71 71
72 /** 72 /**
  73 + * @var array
  74 + */
  75 + protected $headers = [];
  76 +
  77 + /**
73 * App constructor. 78 * App constructor.
74 */ 79 */
75 public function __construct() 80 public function __construct()
@@ -360,10 +365,12 @@ class App { @@ -360,10 +365,12 @@ class App {
360 /** 365 /**
361 * @param $data 366 * @param $data
362 * @param int $http_code 367 * @param int $http_code
  368 + * @param array $headers
  369 + * @return mixed
363 * @author:dc 370 * @author:dc
364 - * @time 2023/3/27 10:53 371 + * @time 2025/8/20 9:53
365 */ 372 */
366 - public static function echo($data, $http_code = 200){ 373 + public static function echo($data, $http_code = 200,array $headers = []){
367 374
368 375
369 if(php_sapi_name()=='cli'){ 376 if(php_sapi_name()=='cli'){
@@ -374,11 +381,18 @@ class App { @@ -374,11 +381,18 @@ class App {
374 381
375 if(is_array($data)){ 382 if(is_array($data)){
376 @header("Content-Type:application/json; charset=UTF-8"); 383 @header("Content-Type:application/json; charset=UTF-8");
377 - echo json_encode($data,JSON_UNESCAPED_UNICODE); 384 + $data = json_encode($data,JSON_UNESCAPED_UNICODE);
378 }else{ 385 }else{
379 @header("Content-Type:text/html; charset=UTF-8"); 386 @header("Content-Type:text/html; charset=UTF-8");
380 - echo $data;  
381 } 387 }
  388 +
  389 + if($headers){
  390 + foreach ($headers as $name=>$value){
  391 + @header($name.':'.$value);
  392 + }
  393 + }
  394 +
  395 + echo $data;
382 } 396 }
383 397
384 public function getError(){ 398 public function getError(){
@@ -386,6 +400,20 @@ class App { @@ -386,6 +400,20 @@ class App {
386 } 400 }
387 401
388 /** 402 /**
  403 + * 输出头信息
  404 + * @param $name
  405 + * @param $value
  406 + * @author:dc
  407 + * @time 2025/8/20 9:50
  408 + */
  409 + public function header($name,$value){
  410 + $name = str_replace('-',' ',$name);
  411 + $name = ucwords($name);
  412 + $name = str_replace(' ','-',$name);
  413 + $this->headers[$name] = $value;
  414 + }
  415 +
  416 + /**
389 * 结束 417 * 结束
390 * @author:dc 418 * @author:dc
391 * @time 2023/2/13 10:54 419 * @time 2023/2/13 10:54
@@ -408,7 +436,7 @@ class App { @@ -408,7 +436,7 @@ class App {
408 $data['debug'] = $last_error; 436 $data['debug'] = $last_error;
409 } 437 }
410 438
411 - self::echo($data,502); 439 + self::echo($data,502,$app->headers);
412 440
413 return true; 441 return true;
414 } 442 }
@@ -420,7 +448,7 @@ class App { @@ -420,7 +448,7 @@ class App {
420 logs($app->getError()); 448 logs($app->getError());
421 } 449 }
422 450
423 - self::echo($app->data); 451 + self::echo($app->data,200,$app->headers);
424 452
425 453
426 // 日志记录 454 // 日志记录