From f7243a133c598e187b2526be6809b8c8c722064d Mon Sep 17 00:00:00 2001 From: wen Date: Wed, 26 Oct 2022 17:04:13 +0800 Subject: [PATCH] rmdir support all sub dirs and files --- controller/ApiController.php | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/controller/ApiController.php b/controller/ApiController.php index 8091286..4b9d446 100644 --- a/controller/ApiController.php +++ b/controller/ApiController.php @@ -215,7 +215,7 @@ Class ApiController extends Controller { } try { - $res = rmdir("{$target}/{$delDir}"); + $res = $this->deleteDirTree( realpath("{$target}/{$delDir}") ); if ($res) { $code = 1; $msg = '目录删除完成'; @@ -397,6 +397,34 @@ Class ApiController extends Controller { return $this->renderJson(compact('code', 'msg', 'err', 'data')); } + protected function deleteDirTree($parentDir) { + if (empty($parentDir)) {return false;} + + $res = true; + + try { + $dir = opendir($parentDir); + + while(false !== ($file = readdir($dir))) { + if ($file != '.' && $file != '..') { + $subpath = "{$parentDir}/{$file}"; + if (is_dir($subpath)) { + $res = $this->deleteDirTree($subpath); + }else { + unlink($subpath); + } + } + } + + closedir($dir); + rmdir($parentDir); + }catch(Excepiton $e) { + $res = false; + } + + return $res; + } + //从runtime/admin/目录里获取管理员当前ip相关的缓存数据 protected function getAdmUserData() { $data = array();