FileUploadService.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace App\Service;
  3. use Illuminate\Support\Facades\Storage;
  4. class FileUploadService extends Service
  5. {
  6. //文件类型
  7. const FILE_TYPE = [
  8. 'txt',
  9. 'jpg',
  10. 'png',
  11. 'gif',
  12. 'jpeg',
  13. 'zip',
  14. 'rar',
  15. 'xlsx',
  16. 'xls'
  17. ];
  18. const tmp_dir = 'kq';
  19. const string = '/image/';
  20. const string_1 = '/api/uploadFiles/';
  21. const string2 = 'qyadmin|';
  22. const string3 = 'qyadmin/';
  23. public function uploadFileForKq($file){
  24. if(empty($file)) return [false, '请上传文件'];
  25. // 获取文件相关信息
  26. $ext = $file->getClientOriginalExtension(); // 扩展名
  27. $realPath = $file->getRealPath(); //临时文件的绝对路径
  28. $ext = strtolower($ext);
  29. if (! in_array($ext, self::FILE_TYPE)){
  30. $str = '文件格式为:';
  31. foreach (self::FILE_TYPE as $value){
  32. $str.= $value . ' ' ;
  33. }
  34. return [false,$str];
  35. }
  36. //文件名
  37. $file_name = date("Ymd").time().rand(1000,9999);
  38. $filename = $file_name . '.' . $ext;
  39. $dir = self::tmp_dir . '/' . $filename;
  40. Storage::disk('public')->put($dir, file_get_contents($realPath));
  41. return [true, self::string . $filename];
  42. }
  43. public function uploadFile($file){
  44. if(empty($file)) return [false, '请上传文件'];
  45. // 获取文件相关信息
  46. $ext = $file->getClientOriginalExtension(); // 扩展名
  47. $realPath = $file->getRealPath(); //临时文件的绝对路径
  48. $ext = strtolower($ext);
  49. if (! in_array($ext, self::FILE_TYPE)){
  50. $str = '文件格式为:';
  51. foreach (self::FILE_TYPE as $value){
  52. $str.= $value . ' ' ;
  53. }
  54. return [false,$str];
  55. }
  56. $date = date("Y-m-d");
  57. //文件名
  58. $file_name = date("Ymd").time().rand(1000,9999);
  59. $filename = $file_name.'.' . $ext;
  60. $dir = self::tmp_dir . '/' . $date . '/' . $filename;
  61. Storage::disk('public')->put($dir, file_get_contents($realPath));
  62. return [true, self::string_1 . self::string2 . $filename];
  63. }
  64. //获取文件的位置oss
  65. public function getFileShow($file_name){
  66. $path = "";
  67. if(empty($file_name)) return $path;
  68. if(strpos($file_name, FileUploadService::string_1 . FileUploadService::string2) !== false){
  69. $file_name = str_replace(FileUploadService::string_1 . FileUploadService::string2,'', $file_name);
  70. $timestamp = substr($file_name, 0, 8); // 截取前八位数字
  71. $date = \DateTime::createFromFormat('Ymd', $timestamp);
  72. $date = $date->format('Y-m-d');
  73. $savePath = self::string3 . $date . '/' . $file_name;
  74. list($status,$path) = (new OssService())->getTemporaryUrl($savePath);
  75. }
  76. return $path;
  77. }
  78. public function uploadFileLocal($file){
  79. if(empty($file)) return [false, '请上传文件'];
  80. // 获取文件相关信息
  81. $ext = $file->getClientOriginalExtension(); // 扩展名
  82. $realPath = $file->getRealPath(); //临时文件的绝对路径
  83. $ext = strtolower($ext);
  84. if (! in_array($ext, self::FILE_TYPE)){
  85. $str = '文件格式为:';
  86. foreach (self::FILE_TYPE as $value){
  87. $str.= $value . ' ' ;
  88. }
  89. return [false,$str];
  90. }
  91. // 上传文件
  92. $file_name = date("Ymd").time().rand(1000,9999);
  93. $filename = $file_name.'.' . $ext;
  94. // 使用我们新建的uploads本地存储空间(目录)
  95. Storage::disk('public')->put('upload_files/'.$filename, file_get_contents($realPath));
  96. return [true, self::string . $filename];
  97. }
  98. public function createOssUpload($file){
  99. if(! is_array($file) && empty($file)) return;
  100. foreach ($file as $filename){
  101. $filename = str_replace(FileUploadService::string_1.FileUploadService::string2,'', $filename);
  102. $timestamp = substr($filename, 0, 8); // 截取前八位数字
  103. $date = \DateTime::createFromFormat('Ymd', $timestamp);
  104. $date = $date->format('Y-m-d');
  105. $dir = FileUploadService::tmp_dir . '/' . $date . '/' . $filename;
  106. $realPath = storage_path() . "/app/public/" . $dir;
  107. $savePath = self::string3 . $date . '/' . $filename;
  108. list($status,$msg) = (new OssService())->uploadFile($realPath,$savePath);
  109. if($status) Storage::disk('public')->delete($dir);
  110. }
  111. }
  112. public function createOssUploadOld($file){
  113. if(! is_array($file) && empty($file)) return;
  114. foreach ($file as $filename){
  115. if(strpos($filename, FileUploadService::string_1.FileUploadService::string2) !== false){
  116. $filename = str_replace(FileUploadService::string_1.FileUploadService::string2,'',$filename);
  117. $timestamp = substr($filename, 0, 8); // 截取前八位数字
  118. $date = \DateTime::createFromFormat('Ymd', $timestamp);
  119. $date = $date->format('Y-m-d');
  120. $delPath = self::string3 . $date . '/' . $filename;
  121. list($status,$msg) = (new OssService())->deleteFile($delPath);
  122. // if(! $status) return [false , $msg];
  123. }
  124. }
  125. }
  126. public function delLocalPublicFile($filename){
  127. $path = storage_path('app/public/kq/' . $filename);
  128. // 将本地路径转换为存储路径
  129. $storagePath = str_replace(storage_path('app/public/'), '', $path);
  130. // 检查文件是否存在
  131. if (Storage::disk('public')->exists($storagePath)) {
  132. // 文件存在,可以进行删除操作
  133. Storage::disk('public')->delete($storagePath);
  134. }
  135. }
  136. }