list_files

Returns a listing of all files in the specified folder and all subdirectories up to 100 levels deep.

Description

(bool|array) list_files( (string) $folder = '', (int) $levels = 100 ); 

The depth of the recursiveness can be controlled by the $levels param.

Returns (bool|array)

False on failure, Else array of files

Parameters (2)

0. $folder — Optional. (string) => ''
Full path to folder. Default empty.
1. $levels — Optional. (int) => 100
Levels of folders to follow, Default 100 (PHP Loop limit).

Usage

  1. if ( !function_exists( 'list_files' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/file.php'; 
  3.  
  4. // Optional. Full path to folder. Default empty. 
  5. $folder = ''; 
  6.  
  7. // Optional. Levels of folders to follow, Default 100 (PHP Loop limit). 
  8. $levels = 100; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = list_files($folder, $levels); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/file.php  
  1. function list_files( $folder = '', $levels = 100 ) { 
  2. if ( empty($folder) ) 
  3. return false; 
  4.  
  5. if ( ! $levels ) 
  6. return false; 
  7.  
  8. $files = array(); 
  9. if ( $dir = @opendir( $folder ) ) { 
  10. while (($file = readdir( $dir ) ) !== false ) { 
  11. if ( in_array($file, array('.', '..') ) ) 
  12. continue; 
  13. if ( is_dir( $folder . '/' . $file ) ) { 
  14. $files2 = list_files( $folder . '/' . $file, $levels - 1); 
  15. if ( $files2 ) 
  16. $files = array_merge($files, $files2 ); 
  17. else 
  18. $files[] = $folder . '/' . $file . '/'; 
  19. } else { 
  20. $files[] = $folder . '/' . $file; 
  21. @closedir( $dir ); 
  22. return $files;