dirs.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. }