FileManager.php 3.4 KB

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