PHP Directory operation Instance Code _php instance

Source: Internet
Author: User
Copy CodeThe code is as follows:
/**
* 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. "
";
}
}
}
Closedir ($DS);
}
Listdir ($dirname);

Core: Recursive classic applications, and basic operations for files and directories.

Copy the Code code as follows:
/**
* 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 the Code code as follows:
/**
* 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 a file with path.

Copy the Code code as follows:
/**
* 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: The recursive function is understood by judging where $tot is returned.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.