Skip to content

Instantly share code, notes, and snippets.

@lgoldstien
Created December 4, 2013 09:07
Show Gist options
  • Save lgoldstien/7784538 to your computer and use it in GitHub Desktop.
Save lgoldstien/7784538 to your computer and use it in GitHub Desktop.
A function to get a list of files from the current directory and all subdirectories, returned in an array.
function getFilesList ($scanDirectory) {
/** Create empty arrays for the files to scan and directories to evaluate */
$_filesToBeScanned = array();
$_directoriesLeftToEvaluate = array();
/** Add the first directoy on to the evaluation queue */
array_push($_directoriesLeftToEvaluate, $scanDirectory);
while (count($_directoriesLeftToEvaluate) !== 0) {
/** Grab the last directory */
$thisIterationsDirectory = array_pop($_directoriesLeftToEvaluate);
/** Operate on every entity in the directory */
foreach (glob($thisIterationsDirectory . "/*") as $entity) {
if (is_dir($entity)) {
/** If the entity is a directory then add it to the evaluation list */
array_push($_directoriesLeftToEvaluate, $entity);
} else if (is_file($entity)) {
/** If the entity is a file then add it to the list of files to be scanned */
array_push($_filesToBeScanned, $entity);
}
}
}
return $_filesToBeScanned;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment