dirs.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace controller;
  3. class dirs extends base
  4. {
  5. public $defaultPath = './';
  6. public function __construct()
  7. {
  8. parent::__construct();
  9. $this->view->assign('defaultPath', $this->defaultPath);
  10. }
  11. public function indexAction()
  12. {
  13. $path = $this->request->get('path', './');
  14. if (!is_dir($path)) {
  15. $this->showErrorPage('指定文件不存在');
  16. return;
  17. }
  18. $files = \helper\tools::getFolders($path);
  19. $usePath = explode('/', $path);
  20. $visitPathes = \helper\tools::getVisitPath($path);
  21. $this->view->assign('visitPathes', $visitPathes);
  22. $currentPath = array_pop($usePath);
  23. $this->view->assign('currentPath', $currentPath);
  24. $this->view->assign('usePath', join('/', $usePath));
  25. $this->view->assign('files', $files);
  26. $this->view->display('manage/folder/dir.html');
  27. }
  28. /**
  29. * 查看指定文件内容,仅限于php、css、js、cpp、.h、java、python类型文件
  30. */
  31. public function fileAction()
  32. {
  33. $file = $this->request->get('file');
  34. if (!is_file($file)) {
  35. $this->showErrorPage($file . '指定文件不存在');
  36. return;
  37. }
  38. //如果是php、css、js、cpp、.h、java、python文件就直接显示内容,否则下载
  39. $extension = pathinfo($file, PATHINFO_EXTENSION);
  40. if (!in_array($extension, array('php', 'css', 'js', 'html', 'cpp', 'h', 'py', 'java', 'ini'))) {
  41. $download = new \Qii\Library\Download();
  42. $download->download($file);
  43. return;
  44. }
  45. $visitPathes = \helper\tools::getVisitPath(pathinfo($file, PATHINFO_DIRNAME));
  46. $this->view->assign('visitPathes', $visitPathes);
  47. $this->view->assign('file', pathinfo($file, PATHINFO_BASENAME));
  48. $usePath = pathinfo($file, PATHINFO_DIRNAME);
  49. $this->view->assign('usePath', $usePath);
  50. $content = file_get_contents($file);
  51. $encode = mb_detect_encoding($content, array('ASCII', 'UTF-8', 'GB2312', 'GBK', 'BIG5'));
  52. if ($encode != 'UTF-8') {
  53. $content = mb_convert_encoding($content, 'UTF-8', $encode);
  54. }
  55. $this->view->assign('code', $content);
  56. $this->view->display('manage/folder/file.html');
  57. }
  58. public function removeAction()
  59. {
  60. $file = $this->request->get('file');
  61. $isAjax = $this->request->get('isAjax', 0);
  62. if (!\helper\tools::allowRemove($file)) {
  63. $isAjax == 1 ? $this->echoJson(array('code' => 1, 'msg' => '此目录或文件不允许删除')) : $this->showTipsPage('此目录或文件不允许删除');
  64. return;
  65. }
  66. $result = \helper\tools::removeFile($file);
  67. if ($result) {
  68. $isAjax == 1 ? $this->echoJson(array('code' => 0, 'msg' => '删除成功')) : $this->showTipsPage('删除成功');
  69. return;
  70. }
  71. $isAjax == 1 ? $this->echoJson(array('code' => 1, 'msg' => '删除失败')) : $this->showTipsPage('删除失败');
  72. }
  73. /**
  74. * 下载指定文件
  75. */
  76. public function downAction()
  77. {
  78. $file = $this->request->get('file');
  79. if (!is_file($file)) {
  80. $this->showErrorPage($file . '指定文件不存在');
  81. return;
  82. }
  83. $download = new \Qii\Library\Download();
  84. $download->download($file);
  85. }
  86. protected function echoJson($data)
  87. {
  88. echo $this->jsonEncode($data);
  89. }
  90. protected function jsonEncode($data)
  91. {
  92. if (empty($data)) return '{}';
  93. if (isset($data['code']) && $data['code'] > 0 && !isset($data['msg'])) $data['msg'] = _i($data['code']);
  94. return json_encode($data, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE);
  95. }
  96. }