SystemFile.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. namespace app\admin\controller\system;
  3. use app\admin\model\system\SystemFile as SystemFileModel;
  4. use app\admin\controller\AuthController;
  5. use service\FormBuilder as Form;
  6. /**
  7. * 文件校验控制器
  8. * Class SystemFile
  9. * @package app\admin\controller\system
  10. *
  11. */
  12. class SystemFile extends AuthController
  13. {
  14. public function index(){
  15. $app = $this->getDir('./application');
  16. $extend = $this->getDir('./extend');
  17. $public = $this->getDir('./public');
  18. $arr = array();
  19. $arr = array_merge($app,$extend);
  20. $arr = array_merge($arr,$public);
  21. $fileAll = array();//本地文件
  22. $cha = array();//不同的文件
  23. foreach ($arr as $k=>$v) {
  24. $fp = fopen($v, 'r');
  25. if (filesize($v)) $ct = fread($fp, filesize($v));
  26. else $ct = null;
  27. fclose($fp);
  28. $cthash = md5($ct);
  29. $update_time = stat($v);
  30. $fileAll[$k]['cthash'] = $cthash;
  31. $fileAll[$k]['filename'] = $v;
  32. $fileAll[$k]['atime'] = $update_time['atime'];
  33. $fileAll[$k]['mtime'] = $update_time['mtime'];
  34. $fileAll[$k]['ctime'] = $update_time['ctime'];
  35. }
  36. $file = SystemFileModel::all(function($query){
  37. $query->order('atime', 'desc');
  38. })->toArray();//数据库中的文件
  39. if(empty($file)){
  40. $data_num = array_chunk($fileAll,10);
  41. SystemFileModel::beginTrans();
  42. $res = true;
  43. foreach ($data_num as $k=>$v){
  44. $res = $res && SystemFileModel::insertAll($v);
  45. }
  46. SystemFileModel::checkTrans($res);
  47. if($res){
  48. $cha = array();//不同的文件
  49. }else{
  50. $cha = $fileAll;
  51. }
  52. }else{
  53. $cha = array();//差异文件
  54. foreach ($file as $k=>$v){
  55. foreach ($fileAll as $ko=>$vo){
  56. if($v['filename'] == $vo['filename']){
  57. if($v['cthash'] != $vo['cthash']){
  58. $cha[$k]['filename'] = $v['filename'];
  59. $cha[$k]['cthash'] = $v['cthash'];
  60. $cha[$k]['atime'] = $v['atime'];
  61. $cha[$k]['mtime'] = $v['mtime'];
  62. $cha[$k]['ctime'] = $v['ctime'];
  63. $cha[$k]['type'] = '已修改';
  64. }
  65. unset($fileAll[$ko]);
  66. unset($file[$k]);
  67. }
  68. }
  69. }
  70. foreach ($file as $k=>$v){
  71. $cha[$k]['filename'] = $v['filename'];
  72. $cha[$k]['cthash'] = $v['cthash'];
  73. $cha[$k]['atime'] = $v['atime'];
  74. $cha[$k]['mtime'] = $v['mtime'];
  75. $cha[$k]['ctime'] = $v['ctime'];
  76. $cha[$k]['type'] = '已删除';
  77. }
  78. foreach ($fileAll as $k=>$v){
  79. $cha[$k]['filename'] = $v['filename'];
  80. $cha[$k]['cthash'] = $v['cthash'];
  81. $cha[$k]['atime'] = $v['atime'];
  82. $cha[$k]['mtime'] = $v['mtime'];
  83. $cha[$k]['ctime'] = $v['ctime'];
  84. $cha[$k]['type'] = '新增的';
  85. }
  86. }
  87. // dump($file);
  88. // dump($fileAll);
  89. $this->assign('cha',$cha);
  90. return $this->fetch();
  91. }
  92. public function filelist(){
  93. $app = $this->getDir('./application');
  94. print_r($app);
  95. $extend = $this->getDir('./extend');
  96. $public = $this->getDir('./public');
  97. $arr = array();
  98. $arr = array_merge($app,$extend);
  99. $arr = array_merge($arr,$public);
  100. $fileAll = array();//本地文件
  101. foreach ($arr as $k=>$v) {
  102. $fp = fopen($v, 'r');
  103. if (filesize($v)) $ct = fread($fp, filesize($v));
  104. else $ct = null;
  105. fclose($fp);
  106. $cthash = md5($ct);
  107. $update_time = stat($v);
  108. $fileAll[$k]['cthash'] = $cthash;
  109. $fileAll[$k]['filename'] = $v;
  110. $fileAll[$k]['atime'] = $update_time['atime'];
  111. $fileAll[$k]['mtime'] = $update_time['mtime'];
  112. $fileAll[$k]['ctime'] = $update_time['ctime'];
  113. }
  114. // dump($file);
  115. dump($fileAll);
  116. }
  117. /**
  118. * 获取文件夹中的文件 不包括子文件
  119. * @param $dir
  120. * @return array
  121. */
  122. public function getNextDir(){
  123. $dir = './';
  124. $list = scandir($dir);
  125. $dirlist = array();
  126. $filelist = array();
  127. foreach($list as $key=>$v) {
  128. if($v !='.' && $v !='..'){
  129. if (is_dir($dir.'/'.$v)) {
  130. $dirlist[$key]['name'] = $v;
  131. $dirlist[$key]['type'] = 'dir';
  132. }
  133. if(is_file($dir.'/'.$v)){
  134. $filelist[$key]['name'] = $v;
  135. $filelist[$key]['type'] = 'file';
  136. }
  137. }
  138. }
  139. $filesarr = array_merge($dirlist,$filelist);
  140. print_r($filesarr);
  141. }
  142. /**
  143. * 获取文件夹中的文件 包括子文件 不能直接用 直接使用 $this->getDir()方法 P156
  144. * @param $path
  145. * @param $data
  146. */
  147. public function searchDir($path,&$data){
  148. if(is_dir($path) && !strpos($path,'uploads')){
  149. $dp=dir($path);
  150. while($file=$dp->read()){
  151. if($file!='.'&& $file!='..'){
  152. $this->searchDir($path.'/'.$file,$data);
  153. }
  154. }
  155. $dp->close();
  156. }
  157. if(is_file($path)){
  158. $data[]=$path;
  159. }
  160. }
  161. /**
  162. * 获取文件夹中的文件 包括子文件
  163. * @param $dir
  164. * @return array
  165. */
  166. public function getDir($dir){
  167. $data=array();
  168. $this->searchDir($dir,$data);
  169. return $data;
  170. }
  171. //测试
  172. public function ceshi(){
  173. //创建form
  174. $form = Form::create('/save.php',[
  175. Form::input('goods_name','商品名称')
  176. ,Form::input('goods_name1','password')->type('password')
  177. ,Form::input('goods_name2','textarea')->type('textarea')
  178. ,Form::input('goods_name3','email')->type('email')
  179. ,Form::input('goods_name4','date')->type('date')
  180. ,Form::cityArea('address','cityArea',[
  181. '陕西省','西安市'
  182. ])
  183. ,Form::dateRange('limit_time','dateRange',
  184. strtotime('- 10 day'),
  185. time()
  186. )
  187. ,Form::dateTime('add_time','dateTime')
  188. ,Form::color('color','color','#ff0000')
  189. ,Form::checkbox('checkbox','checkbox',[1])->options([['value'=>1,'label'=>'白色'],['value'=>2,'label'=>'红色'],['value'=>31,'label'=>'黑色']])
  190. ,Form::date('riqi','date','2018-03-1')
  191. ,Form::dateTimeRange('dateTimeRange','区间时间段')
  192. ,Form::year('year','year')
  193. ,Form::month('month','month')
  194. ,Form::frame('frame','frame','/admin/system.system_attachment/index.html?fodder=frame')
  195. ,Form::frameInputs('frameInputs','frameInputs','/admin/system.system_attachment/index.html?fodder=frameInputs')
  196. ,Form::frameFiles('month1','frameFiles','/admin/system.system_attachment/index.html?fodder=month1')
  197. ,Form::frameImages('fodder1','frameImages','/admin/system.system_attachment/index.html?fodder=fodder1')->maxLength(3)->width('800px')->height('400px')
  198. ,Form::frameImages('fodder11','frameImages','/admin/system.system_attachment/index.html?fodder=fodder11')->icon('images')
  199. ,Form::frameInputOne('month3','frameInputOne','/admin/system.system_attachment/index.html?fodder=month3')->icon('ionic')
  200. ,Form::frameFileOne('month4','frameFileOne','/admin/system.system_attachment/index.html?fodder=month4')
  201. ,Form::frameImageOne('month5','frameImageOne','/admin/system.system_attachment/index.html?fodder=month5')->icon('image')
  202. ,Form::hidden('month6','hidden')
  203. ,Form::number('month7','number')
  204. // ,Form::input input输入框,其他type: text类型Form::text,password类型Form::password,textarea类型Form::textarea,url类型Form::url,email类型Form::email,date类型Form::idate
  205. ,Form::radio('month8','radio')->options([['value'=>1,'label'=>'白色'],['value'=>2,'label'=>'红色'],['value'=>31,'label'=>'黑色']])
  206. ,Form::rate('month9','rate')
  207. ,Form::select('month10','select')->options([['value'=>1,'label'=>'白色'],['value'=>2,'label'=>'红色'],['value'=>31,'label'=>'黑色']])
  208. ,Form::selectMultiple('month11','selectMultiple')
  209. ,Form::selectOne('month12','selectOne')
  210. ,Form::slider('month13','slider',2)
  211. ,Form::sliderRange('month23','sliderRange',2,13)
  212. ,Form::switches('month14','区间时间段')
  213. ,Form::timePicker('month15','区间时间段')
  214. ,Form::time('month16','区间时间段')
  215. ,Form::timeRange('month17','区间时间段')
  216. // ,Form::upload('month','区间时间段')
  217. // ,Form::uploadImages('month','区间时间段')
  218. // ,Form::uploadFiles('month','区间时间段')
  219. // ,Form::uploadImageOne('month','区间时间段')
  220. // ,Form::uploadFileOne('month','区间时间段')
  221. ]);
  222. $html = $form->setMethod('get')->setTitle('编辑商品')->view();
  223. echo $html;
  224. }
  225. }