FileUploadService.php 5.8 KB

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