FileModel.php 904 B

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. namespace app\model;
  3. use think\Model;
  4. class FileModel extends Model
  5. {
  6. protected $name = "file";
  7. protected $pk = "id";
  8. function getPathAttr($value)
  9. {
  10. return joinPath("/", $value);
  11. }
  12. public static function addFile($file, $user_id = null)
  13. {
  14. $originPath = joinPath(public_path(), $file);
  15. if (file_exists($originPath)) {
  16. $info = [];
  17. $info["path"] = $file;
  18. $info["user_id"] = $user_id;
  19. $info['create_time'] = date("Y-m-d H:i:s");
  20. $info['size'] = filesize($originPath);
  21. $info["mime_type"] = mime_content_type($originPath);
  22. self::insert($info);
  23. return $info;
  24. }
  25. return false;
  26. }
  27. function user(): \think\model\relation\HasOne
  28. {
  29. return $this->hasOne(UserModel::class, "id", "user_id")->field("id,nickname,mail");
  30. }
  31. }