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