public static function ListFilteredFiles( string $folder, $filter, bool $recursive = false ) : array
   {

      // Init the array that should contain the resulting file paths
      $files = array();

      if ( ! \is_dir( $folder ) )
      {
         // No folder = we are done here
         return $files;
      }

      if ( ! $recursive )
      {
         // List only files directly contained inside $folder (none from sub folders)

         // Open the directory pointer
         $d = \dir( $folder );
         while ( false !== ( $entry = $d->read() ) )
         {
            if ( $entry == '.' || $entry == '..' )
            {
               // Ignore . and ..
               continue;
            }
            $tmp = Path::Combine( $folder, $entry );
            if ( ! \is_file( $tmp ) )
            {
               continue;
            }
            if ( \is_callable( $filter ) )
            {
               if ( \call_user_func( $filter, $entry, $tmp ) )
               {
                  $files[] = $tmp;
               }
               continue;
            }
            try
            {
               if ( ! \preg_match( $filter, $entry ) )
               {
                  continue;
               }
               $files[] = $tmp;
            }
            catch ( \Exception $ex ) { $ex = null; }
         }
         $d->close();
         return $files;
      }

      static::_listRecursiveFiltered( $files, $filter, $folder );
      return $files;

   }
public static function ListFilteredFiles( string $folder, $filter, bool $recursive = false ) : array
   {

      // Init the array that should contain the resulting file paths
      $files = array();

      if ( ! \is_dir( $folder ) )
      {
         // No folder = we are done here
         return $files;
      }

      if ( ! $recursive )
      {
         // List only files directly contained inside $folder (none from sub folders)

         // Open the directory pointer
         $d = \dir( $folder );
         while ( false !== ( $entry = $d->read() ) )
         {
            if ( $entry == '.' || $entry == '..' )
            {
               // Ignore . and ..
               continue;
            }
            $tmp = Path::Combine( $folder, $entry );
            if ( ! \is_file( $tmp ) )
            {
               continue;
            }
            if ( \is_callable( $filter ) )
            {
               if ( \call_user_func( $filter, $entry, $tmp ) )
               {
                  $files[] = $tmp;
               }
               continue;
            }
            try
            {
               if ( ! \preg_match( $filter, $entry ) )
               {
                  continue;
               }
               $files[] = $tmp;
            }
            catch ( \Exception $ex ) { $ex = null; }
         }
         $d->close();
         return $files;
      }

      static::_listRecursiveFiltered( $files, $filter, $folder );
      return $files;

   }
public static function ListFilteredFiles ( string $ folder , $ filter , bool $ recursive = false ) : array { // Init the array that should contain the resulting file paths $ files = array ( ) ; if ( ! \ is_dir ( $ folder ) ) { // No folder = we are done here return $ files ; } if ( ! $ recursive ) { // List only files directly contained inside $folder (none from sub folders) // Open the directory pointer $ d = \ dir ( $ folder ) ; while ( false !== ( $ entry = $ d -> read ( ) ) ) { if ( $ entry == '.' || $ entry == '..' ) { // Ignore . and .. continue ; } $ tmp = Path :: Combine ( $ folder , $ entry ) ; if ( ! \ is_file ( $ tmp ) ) { continue ; } if ( \ is_callable ( $ filter ) ) { if ( \ call_user_func ( $ filter , $ entry , $ tmp ) ) { $ files [ ] = $ tmp ; } continue ; } try { if ( ! \ preg_match ( $ filter , $ entry ) ) { continue ; } $ files [ ] = $ tmp ; } catch ( \ Exception $ ex ) { $ ex = null ; } } $ d -> close ( ) ; return $ files ; } static :: _listRecursiveFiltered ( $ files , $ filter , $ folder ) ; return $ files ; }
Returns all files, matching the defined filter.

If $recursive is TRUE, it also includes al sub folders.

If the filter is a callback function/method, so it must accept 2 parameters.

- string $itemName The name of the current filter item
- string $itemPath THe absolute path of the item.

@param  string          $folder    The folder
@param  string|callback $filter    Regex or callback|callable for filtering files
@param  boolean         $recursive Include sub folders? (Defaults to FALSE)
@return array
Returns all files matching the defined filter .