php代码怎么删除非空目录
php代码删除非空目录的方法:1、创建一个PHP示例文件;2、设置文件编码为utf8;3、通过递归函数实现删除非空目录即可,其代码如“function deldir($dir){if(file_exists($dir)){$files=scandir($dir);foreach($files as $file){…}}”。
php入门到就业线上直播课:进入学习
Apipost = Postman + Swagger + Mock + Jmeter 超好用的API调试工具:点击使用
本教程操作环境:windows7系统、PHP8.1版、Dell G3电脑。
php代码怎么删除非空目录?
代码如下:
<?php header("Content-type: text/html; charset=utf-8"); $dir='mydir'; function deldir($dir){ if(file_exists($dir)){ $files=scandir($dir); foreach($files as $file){ if($file!='.' && $file!='..'){ $path=$dir.'/'.$file; if(is_dir($path)){ deldir($path); }else{ unlink($path); } } } rmdir($dir); return true; }else{ return false; } } if(deldir($dir)){ echo "目录删除成功!"; }else{ echo "没有目录!"; }
登录后复制