This article mainly introduces the PHP directory operation instance code, need friends can refer to the following
The code is as follows: <?php /** * listdir / header ("content-type:text/h Tml;charset=utf-8 "); $dirname = "./final/factapplication"; function Listdir ($dirname) { $ds = Opendir ($dirname); &NBSP ; while (false!== ($file = Readdir ($ds)) { $path = $dirname. ' /'. $file; if ($file!= '. ' && $file!= ' ... ') { if (Is_dir ($path)) { & nbsp Listdir ($path); } else { & nbsp Echo $file. " <br> "; } } &NBSP; Closedir ($DS); } Listdir ($dirname); Core: The classic application of recursion, as well as the basic operation of files and directories. Code as follows: <?php /** * copydir / $srcdir = ". /fileupload "; $dstdir = "B"; function copydir ($srcdir, $dstdir) { mkdir ($dstdir); $ds = Opendir ($srcdir); while (false!== ($file = Readdir ($ds)) { $p Ath = $srcdir. " /". $file; $dstpath = $dstdir. " /". $file; if ($file!= "." && $file!= "...") { if (Is_dir ($path)) { & nbsp Copydir ($path, $dstpath); } else { &NBSP copy ($path, $dstpath); } } Closedir ($DS); } copydir ($srcdir, $dstdir); Core: copy function. Code as follows: <?php /** * deldir / $dirname = ' a '; function Deldir ($dirname) { $ds = Opendir ($dirname); &NBSP ; while (false!== ($file = Readdir ($ds)) { $path = $dirname. ' /'. $file; if ($file!= '. ' && $file!= ' ... ') { if (Is_dir ($path)) { & nbsp Deldir ($path); &NBSP; } else { unlink ($path); } Closedir ($DS); return rmdir ($dirname); } Deldir ($dirname); Core: Note that unlink deletes the file with path. Code as follows: <?php /** * dirsize / $dirname = "a" ; function dirsize ($dirname) { -static $tot; $DS = Opendir ($dirname); while (false!== ($file = Readdir ($ds)) { $path = $ DirName. ' /'. $file; if ($file!= '. ' && $file!= ' ... ') {   if (Is_dir ($path)) { dirsize ($path); &N Bsp } else { $tot = $ Tot + filesize ($path); } return $tot; Closedir ($DS); } echo dirsize ($dirname); Core: To understand recursive functions by judging where $tot returns.