dirs.php 3.0 KB

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