PHP Directory operation Instance Code _php instance

Source: Internet
Author: User

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.

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.