Oihana PHP

recursiveFilePaths.php

Table of Contents

Functions

recursiveFilePaths()  : array<string|int, mixed>
Recursively retrieves all .php files in a folder (and its subfolders).

Functions

recursiveFilePaths()

Recursively retrieves all .php files in a folder (and its subfolders).

recursiveFilePaths(string $directory[, array<string|int, mixed> $options = [] ]) : array<string|int, mixed>
Parameters
$directory : string

The base path of the file to be scanned.

$options : array<string|int, mixed> = []

The optional parameter to send in the function.

  • excludes (array) : The enumeration of all files to excludes
  • extensions (array) : The optional list of the extensions to use to scan the folder(s).
  • maxDepth (int) : The maximum allowed depth. Default -1 is used
  • sortable (bool) : Indicates if the list of file paths is sorted before returned.
Tags
example

Basic usage: list all PHP files in a directory (and its subdirectories).

use function oihana\files\recursiveFilePaths;

$files = recursiveFilePaths(__DIR__);
foreach ($files as $file) {
echo $file . PHP_EOL;
}

Include only files with certain extensions:

$files = recursiveFilePaths(__DIR__, [
'extensions' => ['php', 'inc'],
]);

Exclude specific filenames from the scan:

$files = recursiveFilePaths(__DIR__, [
'excludes' => ['ignore.php', 'test.php'],
]);

Limit maximum depth of traversal:

$files = recursiveFilePaths(__DIR__, [
'maxDepth' => 1, // Only scan current directory and its direct children
]);

Disable sorting of the resulting file list:

$files = recursiveFilePaths(__DIR__, [
'sortable' => false,
]);

Error handling when scanning an invalid directory:

try {
$files = recursiveFilePaths('invalid/path');
} catch (RuntimeException $e) {
echo "Error: " . $e->getMessage();
}
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
array<string|int, mixed>

The list of the full paths to all files found.


        
On this page

Search results