FileManager.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <<<<<<< HEAD
  2. <?php
  3. declare(strict_types=1);
  4. namespace app\common\model;
  5. use think\facade\Config;
  6. class FileManager extends \think\Model
  7. {
  8. protected $pk = "fileid";
  9. protected $schema = [
  10. 'fileid' => "int", // '文件id'
  11. 'filename' => "varchar", // '文件名称'
  12. 'filesize' => "int", // '文件大小字节'
  13. 'filetime' => "int", // '文件上传时间'
  14. 'filepath' => "varchar", // '文件路径'
  15. 'fileextension' => "varchar", // '文件扩展名'
  16. 'title' => "varchar", // '文件title'
  17. 'type' => "int", // '0为附件,1为图片,2为Flash文件,3为多媒体文件'
  18. 'onclick' => "int", // '下载量'
  19. 'username' => "varchar", // '上传者'
  20. 'cid' => "int", // '栏目ID'
  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')->paginate(['list_rows'=>$limit, 'query' => $param]);
  31. }
  32. public static function saveFileInfo(\think\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->username = 'system';
  48. $fileinfo->hash_md5 = $file->md5();
  49. $fileinfo->save();
  50. }
  51. }
  52. =======
  53. <?php
  54. declare(strict_types=1);
  55. namespace app\common\model;
  56. use think\facade\Config;
  57. use think\File;
  58. use think\exception\FileException;
  59. class FileManager extends \think\Model
  60. {
  61. protected $pk = "fileid";
  62. protected $schema = [
  63. 'fileid' => "int", // '文件id'
  64. 'filename' => "varchar", // '文件名称'
  65. 'filesize' => "int", // '文件大小字节'
  66. 'filetime' => "int", // '文件上传时间'
  67. 'filepath' => "varchar", // '文件路径'
  68. 'fileextension' => "varchar", // '文件扩展名'
  69. 'title' => "varchar", // '文件title'
  70. 'type' => "int", // '0为附件,1为图片,2为Flash文件,3为多媒体文件'
  71. 'onclick' => "int", // '下载量'
  72. 'username' => "varchar", // '上传者'
  73. 'infoid' => "int", // '信息ID',
  74. 'cjid' => "int", // '信息临时ID'
  75. 'hash_md5' => "varcahr" // hash值(MD5)
  76. ];
  77. protected $autoWriteTimestamp = false;
  78. public static function queryPage(array $param = [])
  79. {
  80. $limit = (int)$param['limit'];
  81. $where = [];
  82. return self::where($where)->field('fileid,filename,filesize,filetime,filepath,fileextension,title,username')->order('fileid DESC')->paginate(['list_rows'=>$limit, 'query' => $param]);
  83. }
  84. public static function queryList(array $param = [])
  85. {
  86. $where = [];
  87. $cjid = isset($param['cjid']) ? (int) $param['cjid'] : 0;
  88. $infoid = isset($param['infoid']) ? (int) $param['infoid'] : 0;
  89. if ($cjid) {
  90. $where[] = ['cjid', '=', $cjid];
  91. }
  92. if ($infoid) {
  93. $where[] = ['infoid', '=', $infoid];
  94. }
  95. return self::where($where)->field('fileid,filename,filesize,filetime,filepath,fileextension,title,username')->order('fileid DESC')->select();
  96. }
  97. public static function saveFile(File $file)
  98. {
  99. $fileinfo = self::where('hash_md5', $file->md5())->find();
  100. $publicRootPath = str_replace('\\', '/', Config::get('filesystem.disks.public.root'));
  101. $publicUrlPath = Config::get('filesystem.disks.public.url');
  102. if ($fileinfo == null) {
  103. $fileinfo = new static();
  104. } elseif ($publicRootPath . $fileinfo->filepath == $file->getPathname()) { // 路径不同的文件
  105. $fileinfo = new static();
  106. }
  107. $fileinfo->filename = $file->getFilename();
  108. $fileinfo->filesize = $file->getSize();
  109. $fileinfo->filetime = $file->getCTime();
  110. $fileinfo->filepath = $publicUrlPath . str_replace($publicRootPath, '', $file->getPathname());
  111. $fileinfo->fileextension = $file->getExtension();
  112. $fileinfo->hash_md5 = $file->md5();
  113. $fileinfo->username = 'system';
  114. $fileinfo->save();
  115. }
  116. public static function saveFileInfo($file, $savename, $originalName = '', $id = 0, $cjid = 0, $username = 'system')
  117. {
  118. if (is_string($file)) {
  119. $file = new File($file);
  120. }
  121. if (!$file->isFile()) {
  122. throw new FileException('file not exist');
  123. }
  124. $publicUrlPath = Config::get('filesystem.disks.public.url');
  125. $fileinfo = new static();
  126. $fileinfo->filename = basename($savename);
  127. $fileinfo->filepath = $publicUrlPath . '/'. $savename;
  128. $fileinfo->title = $originalName;
  129. $fileinfo->id = $id;
  130. $fileinfo->cjid = $cjid;
  131. $fileinfo->username = $username;
  132. $fileinfo->filesize = $file->getSize();
  133. $fileinfo->filetime = $file->getCTime();
  134. $fileinfo->fileextension = $file->extension();
  135. $fileinfo->hash_md5 = $file->md5();
  136. $fileinfo->save();
  137. return $fileinfo;
  138. }
  139. public static function saveInfoid($infoid, $cjid)
  140. {
  141. self::where('cjid', 'IN', $cjid)->data(['infoid'=>$infoid, 'cjid'=>0])->update();
  142. }
  143. }
  144. >>>>>>> 78b76253c8ce5873016cf837373af5e30ac80c86