Skip to content

Instantly share code, notes, and snippets.

@ewilan-riviere
Last active August 12, 2024 08:41
Show Gist options
  • Select an option

  • Save ewilan-riviere/ee6a4481bfd50bf86f7eae9d7baa662d to your computer and use it in GitHub Desktop.

Select an option

Save ewilan-riviere/ee6a4481bfd50bf86f7eae9d7baa662d to your computer and use it in GitHub Desktop.
Parse files with PHP
<?php
/**
* Tested with 1290 files: 21.43 sec
*/
private function parseFilesWithLaravelFile(string $path): array
{
$files = \Illuminate\Support\Facades\File::allFiles($path);
return array_map(fn (SplFileInfo $file) => $file->getPathname(), $files);
}
/**
* Tested with 1290 files: 1.97 sec
*/
private function parseFilesWithGlob(string $path, array &$results = []): array
{
$files = glob($path.'/*');
foreach ($files as $file) {
if (is_dir($file)) {
$this->parseFilesWithGlob($file, $results);
} else {
$results[] = $file;
}
}
return $results;
}
/**
* Tested with 1290 files: 4.34 sec
*/
private function parseFilesWithRecursiveIterator(string $path): array
{
$rii = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
$files = [];
/** @var SplFileInfo $file */
foreach ($rii as $file) {
if ($file->isDir()) {
continue;
}
$files[] = $file->getPathname();
}
return $files;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment