FileUploadService.php 6.0 KB

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