FileLocal.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 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;
  13. use think\facade\Filesystem;
  14. use cores\exception\BaseException;
  15. /**
  16. * 写入文件到local(主要处理用户上传的excel文件)
  17. * Class Lock
  18. * @package app\common\library
  19. */
  20. class FileLocal
  21. {
  22. const DISK = 'local';
  23. /**
  24. * 执行文件写入
  25. * @param $file
  26. * @param string $dirName 文件夹名称
  27. * @param int|null $storeId 商城ID
  28. * @return string
  29. * @throws BaseException
  30. */
  31. public static function writeFile(\think\File $file, string $dirName = '', int $storeId = null): string
  32. {
  33. // 文件目录路径
  34. $dirPath = "{$dirName}/{$storeId}";
  35. // 生成文件名
  36. $hash = str_substr(md5(get_guid_v4()), 32);
  37. $fileName = $hash . '.' . $file->extension();
  38. // 写入到本地服务器
  39. $path = Filesystem::disk(self::DISK)->putFileAs($dirPath, $file, $fileName);
  40. empty($path) && throwError('很抱歉,文件写入失败');
  41. // 返回文件完整路径
  42. return runtime_root_path() . self::DISK . "/{$path}";
  43. }
  44. }