FileUploadService.php 5.8 KB

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