PHP readdir Function Usage Example _php skill

Source: Internet
Author: User

This article illustrates the use of Readdir functions in PHP. Share to everyone for your reference. The specific usage analysis is as follows:

Definition and Usage: the Readdir () function returns an entry in a table of contents handle opened by Opendir (), and if successful, the function returns a filename, or false.

Instance one, the code is as follows:

Copy Code code as follows:
$dir = "readdir/";

To determine whether a directory
if (Is_dir ($dir)) {
if ($dh = Opendir ($dir)) {
while (($file = Readdir ($DH))!== false) {
echo "FileName: $file: filetype:". FileType ($dir. $file). " ";
}
Closedir ($DH);
}
}

Instance two, Note that the!== operator does not exist before 4.0.0-RC2,The code is as follows:
Copy Code code as follows:
if ($handle = Opendir ('/path/to/files ')) {
echo "Directory handle: $handle";
echo "Files:";

/* This is the right way to traverse the directory method * *
while (false!== ($file = Readdir ($handle))) {
echo "$file";
}

/* This is the wrong way to traverse the directory * *
while ($file = Readdir ($handle)) {
echo "$file";
}
Closedir ($handle);
}

Instance three, Readdir () will be returned. And.. Items, if you don't want them, just filter it out, Example 2. Lists all files in the current directory and removes them. And.., the code is as follows:
Copy Code code as follows:
if ($handle = Opendir ('. ')) {
while (false!== ($file = Readdir ($handle))) {
if ($file!= "." && $file!= "...") {
echo "$file";
}
}
Closedir ($handle);
}

Note: Readdir must be used in conjunction with Opendir.

I hope this article will help you with your PHP program design.

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.