PHP readdir () Function usage description

Source: Internet
Author: User
The Readdir () function returns the file name of the next file in the directory. Returns the entry name (file name) if successful, and FALSE if it fails.

Grammar

Readdir (Dir_handle);
Parameters Description
Dir_handle

Optional. Specifies the directory handle resource that was previously opened by Opendir ().

If this parameter is not specified, the last link opened by Opendir () is used.

Readdir () Function instance one, the code is as follows:

$dir = "readdir/";  Determines whether the directory if (Is_dir ($dir)) {     if ($dh = Opendir ($dir)) {while         (($file = Readdir ($DH))!== false) {             echo ' fil ename: $file: filetype: ". FileType ($dir. $file). " ";         }         Closedir ($DH);     } }

Readdir () Function instance Two, the 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); }

Readdir () Function 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:

if ($handle = Opendir ('. ')) {while     (false!== ($file = Readdir ($handle))) {         if ($file! = "." && $file! = "...") {             echo "$file";         }     }     Closedir ($handle); }

Note:Readdir must be used in conjunction with Opendir.

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.