ClearUpload.php 952 B

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. namespace app\event;
  3. use app\model\saas\SaasUpload;
  4. class ClearUpload
  5. {
  6. /**
  7. * 清理上传文件
  8. * @param array $param
  9. * @return bool
  10. */
  11. public function clearData(array $param = []): bool
  12. {
  13. try {
  14. if (empty($param['key'])) return true;
  15. $uploadData = (new SaasUpload)->where("key",$param["key"])->select();
  16. if ($uploadData->isEmpty()) return true;
  17. foreach ($uploadData as $file) {
  18. if (file_exists(public_path().$file['path'])) {
  19. unlink(public_path().$file['path']);
  20. }
  21. }
  22. $uploadData->delete(); // 清理数据库
  23. return true;
  24. } catch (\Throwable $th) {
  25. echo "清理上传文件报错\n";
  26. echo $th->getLine()."\n";
  27. echo $th->getFile()."\n";
  28. echo $th->getMessage()."\n";
  29. return false;
  30. }
  31. }
  32. }