Home  >  Article  >  Backend Development  >  Two examples of code for traversing directories in php

Two examples of code for traversing directories in php

WBOY
WBOYOriginal
2016-07-25 08:53:49994browse
  1. /*
  2. * Traverse the directory
  3. * fopen()
  4. * fread()
  5. * fclose()
  6. * @opendir(target path);E_WARNINE
  7. * readdir(directory reference handle) rewinddir ()
  8. * closedir()
  9. *
  10. * In programming, a handle is a special smart pointer. When an application wants to reference memory blocks or objects managed by other systems (such as databases, operating systems)
  11. *, handles are used. The difference between a handle and an ordinary pointer is that
  12. * The pointer contains the memory address of the referenced object, while the handle is a reference identifier managed by the system.
  13. * This identifier can be relocated to a memory address by the system. This mode of indirect object access enhances the system's control over the referenced object.
  14. * (Script Academy bbs.it-home.org)
  15. */
  16. $dirHandle=@opendir("phpMyAdmin") or die("Unsuccessful in opening the directory");
  17. echo "All the contents in the phpMyAdmin directory are: < br>";
  18. echo readdir($dirHandle)."
    ";
  19. echo readdir($dirHandle)."
    ";
  20. while(($file=readdir($dirHandle))!= =false)
  21. {
  22. $file="phpMyadmin".DIRECTORY_SEPARATOR.$file;
  23. if(is_dir($file))
  24. {
  25. echo "Directory: ".$file."
    ";
  26. }else
  27. {
  28. echo "File: ".$file."File size: ".filesize($file)."KB
    ";
  29. }
  30. }
  31. rewinddir($dirHandle); //Return to the beginning of the handle and traverse it again
  32. while(($file=readdir($dirHandle))!==false)
  33. {
  34. $file="phpMyadmin".DIRECTORY_SEPARATOR.$file;
  35. if($file!="."&&$file!=". .") //Do not read directly
  36. {
  37. if(is_dir($file))
  38. {
  39. echo "Directory: ".$file."
    ";
  40. }else
  41. {
  42. echo "File: ". $file."File size: ".filesize($file)."KB
    ";
  43. }
  44. }
  45. }
  46. closedir($dirHandle);
  47. ?>
Copy code

Example 2,

  1. /*
  2. * Traverse directory
  3. *
  4. * class dir{
  5. * string path;
  6. * resource handle;
  7. * string read(void);
  8. * void rewind(void);
  9. * void close(void);
  10. * }
  11. *
  12. * In programming, a handle is a special smart pointer. When an application wants to reference memory blocks or objects managed by other systems (such as databases, operating systems)
  13. *, handles are used. The difference between a handle and an ordinary pointer is that
  14. * The pointer contains the memory address of the referenced object, while the handle is a reference identifier managed by the system.
  15. * This identifier can be relocated to a memory address by the system. This mode of indirect object access enhances the system's control over the referenced object.
  16. *
  17. */
  18. $d = dir("phpMyAdmin");
  19. echo "The path is: ".$d->path."
    ";
  20. echo "The reference handle is: ".$d- >handle."
    ";
  21. // $d->read();
  22. // $d->read();
  23. while(($file=$d->read() )!==false)
  24. {
  25. if($file!="."&&$file!="..")
  26. {
  27. echo $file."
    ";
  28. }
  29. }
  30. $d- >close();
  31. ?>
Copy code


Statement:
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