UploadApi.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace app\common\api;
  3. /**
  4. * Description of UploadApi
  5. *
  6. * @author sgq
  7. */
  8. class UploadApi {
  9. protected $fileSize = 5 * 1024 * 1024;
  10. protected $fileExt = "jpg,png,gif,pdf,xls,xlsx";
  11. protected $fileMime = "image/jpg,image/png,image/gif,application/pdf,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
  12. public function __construct($config = []) {
  13. $this->fileSize = isset($config["maxSize"]) ? $config["maxSize"] : $this->fileSize;
  14. $this->fileExt = isset($config["fileExt"]) ? $config["fileExt"] : $this->fileExt;
  15. $this->fileMime = isset($config["fileMime"]) ? $config["fileMime"] : $this->fileMime;
  16. }
  17. /**
  18. * 批量上传文件
  19. * @param type $files
  20. * @return type
  21. */
  22. public function uploadList($files, $path = "uploadfiles") {
  23. try {
  24. validate(['image' => ["sileSize" => $this->fileSize, "fileExt" => $this->fileExt, "fileMime" => $this->fileMime]])
  25. ->check($files);
  26. $savename = [];
  27. foreach ($files as $file) {
  28. $savename[] = \think\facade\Filesystem::disk("public")->putFile($path, $file);
  29. }
  30. return $savename;
  31. } catch (\think\exception\ValidateException $e) {
  32. echo $e->getMessage();
  33. }
  34. }
  35. /**
  36. * 单个文件上传
  37. * @param type $file
  38. * @return type
  39. */
  40. public function uploadOne($file, $path = "uploadfiles") {
  41. try {
  42. validate(['image' => ["sileSize" => $this->fileSize, "fileExt" => $this->fileExt, "fileMime" => $this->fileMime]])
  43. ->check([$file]);
  44. $savename = \think\facade\Filesystem::disk("public")->putFile($path, $file);
  45. return $savename;
  46. } catch (\think\exception\ValidateException $e) {
  47. echo $e->getMessage();
  48. }
  49. }
  50. }