An introductory tutorial on how to use the php readdir function

WBOY
Release: 2016-07-25 08:52:23
Original
881 people have browsed it
  1. $dir = "readdir/";
  2. // Determine whether it is a directory
  3. if (is_dir($dir)) {
  4. if ($dh = opendir($dir)) {
  5. while (($file = readdir($dh)) !== false) {
  6. echo "filename: $file : filetype: " . filetype($dir . $file) . " ";
  7. }
  8. closedir($dh);
  9. }
  10. }
Copy the code

Example 2, note that the !== operator does not exist before 4.0.0-RC2.

  1. if ($handle = opendir('/path/to/files')) {
  2. echo "Directory handle: $handle ";
  3. echo "Files: ";
  4. /* This is the correct traversal Directory method*/
  5. while (false !== ($file = readdir($handle))) {
  6. echo "$file ";
  7. }
  8. /* This is the wrong way to traverse the directory*/
  9. while ($ file = readdir($handle)) {
  10. echo "$file ";
  11. }
  12. closedir($handle);
  13. }
Copy code

Example 3, readdir() will return . and .. entries, Just filter it out if you don't want to use it. Example 2. List all files in the current directory and remove . and....

  1. if ($handle = opendir('.')) {
  2. while (false !== ($file = readdir($handle))) {
  3. if ($file != "." && $ file != "..") {
  4. echo "$file ";
  5. }
  6. }
  7. closedir($handle);
  8. }
Copy code

Explanation, readdir must be used in conjunction with opendir.



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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!