FileUploadService.php 6.2 KB

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