Copy Code code as follows:
<?php
/**
* Listdir
*/
Header ("Content-type:text/html;charset=utf-8");
$dirname = "./final/factapplication";
function Listdir ($dirname) {
$ds = Opendir ($dirname);
while (false!== ($file = Readdir ($ds))) {
$path = $dirname. ' /'. $file;
if ($file!= '. ' && $file!= ' ... ') {
if (Is_dir ($path)) {
Listdir ($path);
} else {
echo $file. " <br> ";
}
}
}
Closedir ($DS);
}
Listdir ($dirname);
Core: The classic application of recursion, as well as the basic operation of files and directories.
Copy Code code as follows:
<?php
/**
* Copydir
*/
$srcdir = ". /fileupload ";
$dstdir = "B";
function Copydir ($srcdir, $dstdir) {
mkdir ($dstdir);
$ds = Opendir ($srcdir);
while (false!== ($file = Readdir ($ds))) {
$path = $srcdir. " /". $file;
$dstpath = $dstdir. " /". $file;
if ($file!= "." && $file!= "...") {
if (Is_dir ($path)) {
Copydir ($path, $dstpath);
} else {
Copy ($path, $dstpath);
}
}
}
Closedir ($DS);
}
Copydir ($srcdir, $dstdir);
Core: copy function.
Copy Code code as follows:
<?php
/**
* Deldir
*/
$dirname = ' a ';
function Deldir ($dirname) {
$ds = Opendir ($dirname);
while (false!== ($file = Readdir ($ds))) {
$path = $dirname. ' /'. $file;
if ($file!= '. ' && $file!= ' ... ') {
if (Is_dir ($path)) {
Deldir ($path);
} else {
Unlink ($path);
}
}
}
Closedir ($DS);
return rmdir ($dirname);
}
Deldir ($dirname);
Core: Note that unlink deletes the file with path.
Copy Code 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);
} else {
$tot = $tot + filesize ($path);
}
}
}
return $tot;
Closedir ($DS);
}
echo dirsize ($dirname);
Core: Understand recursive functions by determining where $tot return.