Local.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\common\library\storage\engine;
  13. use think\facade\Filesystem;
  14. use app\common\library\storage\FileValidate;
  15. /**
  16. * 本地文件驱动
  17. * Class Local
  18. * @package app\common\library\storage\drivers
  19. */
  20. class Local extends Basics
  21. {
  22. /**
  23. * 上传图片文件
  24. * @return array|bool
  25. */
  26. public function upload()
  27. {
  28. // 验证文件类型
  29. if (!$this->validate()) {
  30. return false;
  31. }
  32. try {
  33. $filePath = $this->getSaveFileInfo()['file_path'];
  34. // 上传到本地服务器
  35. $sts = Filesystem::disk($this->disk)->putFileAs(
  36. $this->getFileHashRoute($filePath),
  37. $this->file,
  38. $this->getFileHashName($filePath)
  39. );
  40. return (bool)$sts;
  41. } catch (\Exception $e) {
  42. $this->error = $e->getMessage();
  43. return false;
  44. }
  45. }
  46. /**
  47. * 验证上传的文件
  48. * @return bool
  49. */
  50. private function validate()
  51. {
  52. $FileValidate = new FileValidate;
  53. if (!$FileValidate->check([$this->validateRuleScene => $this->file])) {
  54. $this->error = $FileValidate->getError();
  55. return false;
  56. }
  57. return true;
  58. }
  59. /**
  60. * 删除文件
  61. * @param string $filePath
  62. * @return bool|mixed
  63. */
  64. public function delete(string $filePath)
  65. {
  66. // 文件所在目录
  67. $realPath = realpath(web_path() . "uploads/{$filePath}");
  68. return $realPath === false || unlink($realPath);
  69. }
  70. }