FileManager.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. declare(strict_types=1);
  3. namespace app\common\model;
  4. use think\facade\Config;
  5. class FileManager extends \think\Model
  6. {
  7. protected $pk = "fileid";
  8. protected $schema = [
  9. 'fileid' => "int", // '文件id'
  10. 'filename' => "varchar", // '文件名称'
  11. 'filesize' => "int", // '文件大小字节'
  12. 'filetime' => "int", // '文件上传时间'
  13. 'filepath' => "varchar", // '文件路径'
  14. 'fileextension' => "varchar", // '文件扩展名'
  15. 'title' => "varchar", // '文件title'
  16. 'type' => "int", // '0为附件,1为图片,2为Flash文件,3为多媒体文件'
  17. 'onclick' => "int", // '下载量'
  18. 'username' => "varchar", // '上传者'
  19. 'cid' => "int", // '栏目ID'
  20. 'infoid' => "int", // '信息ID',
  21. 'cjid' => "int", // '信息临时ID'
  22. 'hash_md5' => "varcahr" // hash值(MD5)
  23. ];
  24. protected $autoWriteTimestamp = false;
  25. public static function queryPage(array $param = [])
  26. {
  27. $limit = (int)$param['limit'];
  28. $where = [];
  29. return self::where($where)->field('fileid,filename,filesize,filetime,filepath,fileextension,title,username')->paginate(['list_rows'=>$limit, 'query' => $param]);
  30. }
  31. public static function saveFileInfo(\think\File $file)
  32. {
  33. $fileinfo = self::where('hash_md5', $file->md5())->find();
  34. $publicRootPath = str_replace('\\', '/', Config::get('filesystem.disks.public.root'));
  35. $publicUrlPath = Config::get('filesystem.disks.public.url');
  36. if ($fileinfo == null) {
  37. $fileinfo = new static();
  38. } elseif ($publicRootPath . $fileinfo->filepath == $file->getPathname()) { // 路径不同的文件
  39. $fileinfo = new static();
  40. }
  41. $fileinfo->filename = $file->getFilename();
  42. $fileinfo->filesize = $file->getSize();
  43. $fileinfo->filetime = $file->getCTime();
  44. $fileinfo->filepath = $publicUrlPath . str_replace($publicRootPath, '', $file->getPathname());
  45. $fileinfo->fileextension = $file->getExtension();
  46. $fileinfo->username = 'system';
  47. $fileinfo->hash_md5 = $file->md5();
  48. $fileinfo->save();
  49. }
  50. }