PHP readdir Function Usage example, Readdir function Instance _php Tutorial

Source: Internet
Author: User

PHP readdir Function Usage example, Readdir function instance


This example describes 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 directory handle opened by Opendir (), and if successful, returns a file name, otherwise false.

Example one, the code is as follows:
Copy CodeThe code is as follows: $dir = "readdir/";

Determine if the directory
if (Is_dir ($dir)) {
if ($dh = Opendir ($dir)) {
while (($file = Readdir ($DH))!== false) {
echo "FileName: $file: filetype:". FileType ($dir. $file). " ";
}
Closedir ($DH);
}
}
Example Two,Note that the!== operator does not exist before 4.0.0-rc2.The code is as follows:
Copy CodeThe code is as follows: if ($handle = Opendir ('/path/to/files ')) {
echo "Directory handle: $handle";
echo "Files:";

/* This is the correct 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.. Entries, if you do not want them, just filter out, example 2. List all files in the current directory and remove them. And.., the code is as follows:
Copy CodeThe code is 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 is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/910592.html www.bkjia.com true http://www.bkjia.com/PHPjc/910592.html techarticle PHP's Readdir function Usage example, Readdir Function Example This article describes the Readdir function usage in PHP. Share to everyone for your reference. The specific usage analysis is as follows: definition and usage: ...

  • 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.