SystemFile.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. use think\Request;
  7. use service\FileService as FileClass;
  8. use service\JsonService as Json;
  9. /**
  10. * 文件校验控制器
  11. * Class SystemFile
  12. * @package app\admin\controller\system
  13. *
  14. */
  15. class SystemFile extends AuthController
  16. {
  17. //打开目录
  18. public function opendir($filedir=''){
  19. $fileAll = array('dir'=>[],'file'=>[]);
  20. if(Request::instance()->param('superior') && !empty(Request::instance()->param('dir'))){
  21. $path = './'.Request::instance()->param('dir');
  22. $path = dirname($path);
  23. }else{
  24. $path = !empty(Request::instance()->param('dir'))?Request::instance()->param('dir'):'.';
  25. $path = $path.'/'.Request::instance()->param('filedir');
  26. }
  27. $list = scandir($path);
  28. foreach($list as $key=>$v) {
  29. if($v !='.' && $v !='..'){
  30. if (is_dir($path.'/'.$v)) {
  31. $fileAll['dir'][] = FileClass::list_info($path.'/'.$v);
  32. }
  33. if(is_file($path.'/'.$v)){
  34. $fileAll['file'][] = FileClass::list_info($path.'/'.$v);
  35. }
  36. }
  37. }
  38. // var_dump($fileAll['file']);
  39. //兼容windows
  40. $uname=php_uname('s');
  41. if(strstr($uname,'Windows')!==false) $path = ltrim($path,'\\');
  42. $dir = ltrim($path,'./');
  43. $this->assign(compact('fileAll','dir'));
  44. return $this->fetch();
  45. }
  46. //读取文件
  47. public function openfile($file='')
  48. {
  49. $file = $this->request->param('file');
  50. if(empty($file))return Json::fail('出现错误');
  51. $filepath = '.'.DS.$file;
  52. $content = htmlspecialchars(FileClass::read_file($filepath));//防止页面内嵌textarea标签
  53. $ext = FileClass::get_ext($filepath);
  54. $extarray = [
  55. 'js'=>'text/javascript'
  56. ,'php'=>'text/x-php'
  57. ,'html'=>'text/html'
  58. ,'sql'=>'text/x-mysql'
  59. ,'css'=>'text/x-scss'];
  60. $mode = empty($extarray[$ext])?'':$extarray[$ext];
  61. $this->assign(compact('content','mode','filepath'));
  62. return $this->fetch();
  63. }
  64. //保存文件
  65. public function savefile(){
  66. $comment = $this->request->post('comment');
  67. $filepath = $this->request->post('filepath');
  68. if(!empty($comment) && !empty($filepath)){
  69. //兼容windows
  70. $uname=php_uname('s');
  71. if(strstr($uname,'Windows')!==false)
  72. $filepath = ltrim(str_replace('/', DS, $filepath),'.');
  73. $res = FileClass::write_file($filepath,$comment);
  74. if($res){
  75. return Json::successful('保存成功!');
  76. }else{
  77. return Json::fail('保存失败');
  78. }
  79. }else{
  80. return Json::fail('出现错误');
  81. }
  82. }
  83. public function index(){
  84. $app = $this->getDir('./application');
  85. $extend = $this->getDir('./extend');
  86. $public = $this->getDir('./public');
  87. $arr = array();
  88. $arr = array_merge($app,$extend);
  89. $arr = array_merge($arr,$public);
  90. $fileAll = array();//本地文件
  91. $cha = array();//不同的文件
  92. foreach ($arr as $k=>$v) {
  93. $fp = fopen($v, 'r');
  94. if (filesize($v)) $ct = fread($fp, filesize($v));
  95. else $ct = null;
  96. fclose($fp);
  97. $cthash = md5($ct);
  98. $update_time = stat($v);
  99. $fileAll[$k]['cthash'] = $cthash;
  100. $fileAll[$k]['filename'] = $v;
  101. $fileAll[$k]['atime'] = $update_time['atime'];
  102. $fileAll[$k]['mtime'] = $update_time['mtime'];
  103. $fileAll[$k]['ctime'] = $update_time['ctime'];
  104. }
  105. $file = SystemFileModel::all(function($query){
  106. $query->order('atime', 'desc');
  107. })->toArray();//数据库中的文件
  108. if(empty($file)){
  109. $data_num = array_chunk($fileAll,10);
  110. SystemFileModel::beginTrans();
  111. $res = true;
  112. foreach ($data_num as $k=>$v){
  113. $res = $res && SystemFileModel::insertAll($v);
  114. }
  115. SystemFileModel::checkTrans($res);
  116. if($res){
  117. $cha = array();//不同的文件
  118. }else{
  119. $cha = $fileAll;
  120. }
  121. }else{
  122. $cha = array();//差异文件
  123. foreach ($file as $k=>$v){
  124. foreach ($fileAll as $ko=>$vo){
  125. if($v['filename'] == $vo['filename']){
  126. if($v['cthash'] != $vo['cthash']){
  127. $cha[$k]['filename'] = $v['filename'];
  128. $cha[$k]['cthash'] = $v['cthash'];
  129. $cha[$k]['atime'] = $v['atime'];
  130. $cha[$k]['mtime'] = $v['mtime'];
  131. $cha[$k]['ctime'] = $v['ctime'];
  132. $cha[$k]['type'] = '已修改';
  133. }
  134. unset($fileAll[$ko]);
  135. unset($file[$k]);
  136. }
  137. }
  138. }
  139. foreach ($file as $k=>$v){
  140. $cha[$k]['filename'] = $v['filename'];
  141. $cha[$k]['cthash'] = $v['cthash'];
  142. $cha[$k]['atime'] = $v['atime'];
  143. $cha[$k]['mtime'] = $v['mtime'];
  144. $cha[$k]['ctime'] = $v['ctime'];
  145. $cha[$k]['type'] = '已删除';
  146. }
  147. foreach ($fileAll as $k=>$v){
  148. $cha[$k]['filename'] = $v['filename'];
  149. $cha[$k]['cthash'] = $v['cthash'];
  150. $cha[$k]['atime'] = $v['atime'];
  151. $cha[$k]['mtime'] = $v['mtime'];
  152. $cha[$k]['ctime'] = $v['ctime'];
  153. $cha[$k]['type'] = '新增的';
  154. }
  155. }
  156. // dump($file);
  157. // dump($fileAll);
  158. $this->assign('cha',$cha);
  159. return $this->fetch();
  160. }
  161. /**
  162. * 获取文件夹中的文件 不包括子文件
  163. * @param $dir
  164. * @return array
  165. */
  166. public function getNextDir(){
  167. $dir = './';
  168. $list = scandir($dir);
  169. $dirlist = array();
  170. $filelist = array();
  171. foreach($list as $key=>$v) {
  172. if($v !='.' && $v !='..'){
  173. if (is_dir($dir.'/'.$v)) {
  174. $dirlist['dir'][$key] = $v;
  175. }
  176. if(is_file($dir.'/'.$v)){
  177. $filelist['file'][$key] = $v;
  178. }
  179. }
  180. }
  181. $filesarr = array_merge($dirlist,$filelist);
  182. print_r($filesarr);
  183. }
  184. /**
  185. * 获取文件夹中的文件 包括子文件 不能直接用 直接使用 $this->getDir()方法 P156
  186. * @param $path
  187. * @param $data
  188. */
  189. public function searchDir($path,&$data){
  190. if(is_dir($path) && !strpos($path,'uploads')){
  191. $dp=dir($path);
  192. while($file=$dp->read()){
  193. if($file!='.'&& $file!='..'){
  194. $this->searchDir($path.'/'.$file,$data);
  195. }
  196. }
  197. $dp->close();
  198. }
  199. if(is_file($path)){
  200. $data[]=$path;
  201. }
  202. }
  203. /**
  204. * 获取文件夹中的文件 包括子文件
  205. * @param $dir
  206. * @return array
  207. */
  208. public function getDir($dir){
  209. $data=array();
  210. $this->searchDir($dir,$data);
  211. return $data;
  212. }
  213. //测试
  214. public function ceshi(){
  215. //创建form
  216. $form = Form::create('/save.php',[
  217. Form::input('goods_name','商品名称')
  218. ,Form::input('goods_name1','password')->type('password')
  219. ,Form::input('goods_name2','textarea')->type('textarea')
  220. ,Form::input('goods_name3','email')->type('email')
  221. ,Form::input('goods_name4','date')->type('date')
  222. ,Form::city('address','cityArea',
  223. '陕西省','西安市'
  224. )
  225. ,Form::dateRange('limit_time','dateRange',
  226. strtotime('- 10 day'),
  227. time()
  228. )
  229. ,Form::dateTime('add_time','dateTime')
  230. ,Form::color('color','color','#ff0000')
  231. ,Form::checkbox('checkbox','checkbox',[1])->options([['value'=>1,'label'=>'白色'],['value'=>2,'label'=>'红色'],['value'=>31,'label'=>'黑色']])
  232. ,Form::date('riqi','date','2018-03-1')
  233. ,Form::dateTimeRange('dateTimeRange','区间时间段')
  234. ,Form::year('year','year')
  235. ,Form::month('month','month')
  236. ,Form::frame('frame','frame','/admin/system.system_attachment/index.html?fodder=frame')
  237. ,Form::frameInputs('frameInputs','frameInputs','/admin/system.system_attachment/index.html?fodder=frameInputs')
  238. ,Form::frameFiles('month1','frameFiles','/admin/system.system_attachment/index.html?fodder=month1')
  239. ,Form::frameImages('fodder1','frameImages','/admin/system.system_attachment/index.html?fodder=fodder1')->maxLength(3)->width('800px')->height('400px')
  240. ,Form::frameImages('fodder11','frameImages','/admin/system.system_attachment/index.html?fodder=fodder11')->icon('images')
  241. ,Form::frameInputOne('month3','frameInputOne','/admin/system.system_attachment/index.html?fodder=month3')->icon('ionic')
  242. ,Form::frameFileOne('month4','frameFileOne','/admin/system.system_attachment/index.html?fodder=month4')
  243. ,Form::frameImageOne('month5','frameImageOne','/admin/system.system_attachment/index.html?fodder=month5')->icon('image')
  244. ,Form::hidden('month6','hidden')
  245. ,Form::number('month7','number')
  246. // ,Form::input input输入框,其他type: text类型Form::text,password类型Form::password,textarea类型Form::textarea,url类型Form::url,email类型Form::email,date类型Form::idate
  247. ,Form::radio('month8','radio')->options([['value'=>1,'label'=>'白色'],['value'=>2,'label'=>'红色'],['value'=>31,'label'=>'黑色']])
  248. ,Form::rate('month9','rate')
  249. ,Form::select('month10','select')->options([['value'=>1,'label'=>'白色'],['value'=>2,'label'=>'红色'],['value'=>31,'label'=>'黑色']])
  250. ,Form::selectMultiple('month11','selectMultiple')
  251. ,Form::selectOne('month12','selectOne')
  252. ,Form::slider('month13','slider',2)
  253. ,Form::sliderRange('month23','sliderRange',2,13)
  254. ,Form::switches('month14','区间时间段')
  255. ,Form::timePicker('month15','区间时间段')
  256. ,Form::time('month16','区间时间段')
  257. ,Form::timeRange('month17','区间时间段')
  258. // ,Form::upload('month','区间时间段')
  259. // ,Form::uploadImages('month','区间时间段')
  260. // ,Form::uploadFiles('month','区间时间段')
  261. // ,Form::uploadImageOne('month','区间时间段')
  262. // ,Form::uploadFileOne('month','区间时间段')
  263. ]);
  264. $html = $form->setMethod('get')->setTitle('编辑商品')->view();
  265. echo $html;
  266. }
  267. }