mkdir in PHP create multilevel catalogs
The code is as follows |
Copy Code |
function Mkdirs ($dir) {if (!is_dir ($dir)) {if (!mkdirs (DirName ($dir))) {return false; } if (!mkdir ($dir, 0777)) {return false; } } return true; }
|
Call method
Mkdirs (' div/css/layout ');
A more standard way to create multilevel catalogs
The code is as follows |
Copy Code |
Check and create multilevel catalogs function Checkdir ($path) { $pathArray = explode ('/', $path); $nowPath = '; Array_pop ($pathArray); foreach ($pathArray as $key => $value) { if (' = = $value ') { Unset ($pathArray [$key]); }else{ if ($key = = 0) $nowPath. = $value; Else $nowPath. = '/'. $value; if (!is_dir ($nowPath)) { if (!mkdir ($nowPath, 0777)) return false; } } } return true; } |
Delete a multilevel directory method
Test successfully under WinXP, as long as the PHP file encoding for gb2312, file name arbitrary, should be changed to encode the file name gb2312, on the line, no test
The code is as follows |
Copy Code |
<?php Header ("content-type:text/html; charset=gb2312 "); if (Deletedir ('./complex complex) complex complex complex complex parts duplicate complex duplicate piece complex) echo "Delete Success"; function Deletedir ($dir) { if (@rmdir ($dir) ==false && is_dir ($dir))//deleted, go to delete all files { if ($DP = Opendir ($dir)) { while (($file =readdir ($DP))!= false) { if ($file!= '. ' && $file!= ' ... ') {//echo $file = $dir. '/'. $file; Echo ' <br/> '; $file = $dir. '/'. $file; if (Is_dir ($file))//is the real directory { Deletedir ($file); }else { Unlink ($file); } } } Closedir ($DP); }else { return false; } } if (Is_dir ($dir) && @rmdir ($dir) ==false)//is directory is not deleted return false; return true; } ?> |
Recursively Delete multilevel directories
In the same vein, PHP uses RmDir and unlink to recursively delete the multilevel directory code:
code is as follows |
copy code |
function Rmdirs ($dir) { $d = Dir ($dir); while (false!== ($child = $d->read ()) { if ( $child!= '. ' && $child!= '. ') { if (Is_dir ($dir. '/$child)) Rmdirs ($dir. '/'. $child); Else unlink ($dir. '/'. $child); } } $d->close (); The //Call method is also very simple as long as the directory is good RmDir ($dir); } |