Last active
          December 8, 2016 09:02 
        
      - 
      
- 
        Save solicomo/6522dde5b40761a2c2749c9ea5b185df to your computer and use it in GitHub Desktop. 
    [PHP] get all files in a path
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <?php | |
| function files_in($path, $types = 'f', $recursive = false) | |
| { | |
| $files = array(); | |
| //$path = realpath($path); | |
| $dirs = new RecursiveDirectoryIterator($path); | |
| $dirs->setFlags($dirs->getFlags() | FilesystemIterator::SKIP_DOTS); | |
| if ($recursive) { | |
| $dirs = new RecursiveIteratorIterator($dirs, RecursiveIteratorIterator::SELF_FIRST); // SELF_FIRST is important if you also want Dir children | |
| } | |
| foreach ($dirs as $name => $dir) { | |
| if ($dir->isDir() && strpos($types, 'd') !== false) { | |
| $files[] = $name; | |
| } | |
| if ($dir->isFile() && strpos($types, 'f') !== false) { | |
| $files[] = $name; | |
| } | |
| } | |
| return $files; | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment