(PHP源码)删除指定目录

<?php

deleteDir('123/');//删除123目录

function deleteDir($dirPath) {
    if(!is_dir($dirPath)){
		$file_del=[];
		$file_del['code']=404;
		$file_del['msg']="目录《".$dirPath."》不存在!无需删除!";
		$json=json_encode($file_del,JSON_UNESCAPED_UNICODE);
		print_r($json);
		return;
    }
    if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') {
        $dirPath .= '/';
    }
    $files = glob($dirPath . '*', GLOB_MARK);
    foreach ($files as $file) {
        if (is_dir($file)) {
            deleteDir($file);
        } else {
            unlink($file);
        }
    }
    rmdir($dirPath);
	$file_del=[];
	$file_del['code']=200;
	$file_del['msg']="目录《".$dirPath."》删除成功";
	$json=json_encode($file_del,JSON_UNESCAPED_UNICODE);
	print_r($json);
	return;
}

?>

 

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享
评论 抢沙发

请登录后发表评论