php创建级联目录以及删除级联目录

1、mkdir php5.0起新增一个功能,能够创建级联目录

bool mkdir ( string $pathname [, int $mode = 0777 [, bool $recursive = false [, resource $context ]]] )

默认的 mode 是 0777,意味着最大可能的访问权。有关 mode 的更多信息请阅读 chmod() 页面。 

Allows the creation of nested directories specified in the pathname

例如:echo mkdir("331a/bbb/ddd",0777,true)?"ok":"false";

echo mkdir("./aa/d/dfjksda/dsaf/dfa/da/ds",0777,true)?"ok1":"false";

 

 

2、递归删除级联目录

/*
1、先判断是不是目录
2、如果是则打开目录
3、while循环读取目录
4、判断读取的目录$orw 是否等于 . 或者 ..
5、如果 $row 是文件则删除
6、如果是目录则调用自身函数
7、删除目录
*/
function
deldir($path){ if (!is_dir($path)) { //不是目录返回Null # code... return null; } $dh = opendir($path); //打开目录 while (($row = readdir($dh)) !== false) { //读取目录 # code... if ($row=="." || $row=="..") { # code... continue; } if (!is_dir($path."/".$row)) { //如果子级是文件 # code... unlink($path."/".$row); }else{ //如果子级是目录,递归调用自身 deldir($path.'/'.$row); } } closedir($dh); //关闭目录 return rmdir($path); //删除目录 } echo deldir("./a");

 

posted @ 2015-02-05 13:28  人间最美二月天  阅读(276)  评论(0编辑  收藏  举报