FileModel.php 963 B

1234567891011121314151617181920212223242526272829303132333435363738
  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['hash'] = hash_file("md5", $originPath);
  22. $info["mime_type"] = mime_content_type($originPath);
  23. self::insert($info);
  24. return $info;
  25. }
  26. return false;
  27. }
  28. function user(): \think\model\relation\HasOne
  29. {
  30. return $this->hasOne(UserModel::class, "id", "user_id")->field("id,nickname,mail");
  31. }
  32. }