Home  >  Article  >  Backend Development  >  PHP lists all files and folders in a directory

PHP lists all files and folders in a directory

WBOY
WBOYOriginal
2016-07-25 08:42:441029browse

Use the following PHP code snippet to list all files and folders in a directory

  1. function list_files($dir)
  2. {
  3. if(is_dir($dir))
  4. {
  5. if($handle = opendir ($dir))
  6. {
  7. while(($file = readdir($handle)) !== false)
  8. {
  9. if($file != "." && $file != ".." & ;& $file != "Thumbs.db"/*pesky windows, images..*/)
  10. {
  11. echo '
    '."n";
  12. }
  13. }
  14. closedir($handle);
  15. }
  16. }
  17. }
Copy code

Usage:

  1. list_files("images/"); //This will list all files of images folder
  2. ?>
Copy code

PHP


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