Home > Backend Development > PHP Tutorial > PHP readdir function usage example, readdir function example_PHP tutorial

PHP readdir function usage example, readdir function example_PHP tutorial

WBOY
Release: 2016-07-13 10:14:14
Original
737 people have browsed it

PHP readdir function usage example, readdir function example

The example in this article describes the usage of readdir function in php. Share it with everyone for your reference. The specific usage analysis is as follows:

Definition and usage: The readdir() function returns the entry in the directory handle opened by opendir(). If successful, the function returns a file name, otherwise it returns false.

Example 1, the code is as follows:

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

// Determine whether it is a directory
if (is_dir($dir)) {
If ($dh = opendir($dir)) {
While (($file = readdir($dh)) !== false) {
echo "filename: $file : filetype: " . filetype($dir . $file) . " ";
          }
        closedir($dh);
}
}

Example 2, Note that the !== operator did not exist before 4.0.0-RC2, The code is as follows:
Copy code 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 */
While (false !== ($file = readdir($handle))) {
            echo "$file ";
}

/* This is the wrong way to traverse the directory */
While ($file = readdir($handle)) {
            echo "$file ";
}
closedir($handle);
}

Example 3, readdir() will return . and .. entries. If you don’t want them, just filter them out. Example 2. List all files in the current directory and remove . and .., the code is as follows:
Copy code 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.

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/910592.htmlTechArticlePHP’s readdir function usage example, readdir function example This article describes the use of the readdir function in php. Share it with everyone for your reference. The specific usage analysis is as follows: Definition and usage:...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template