Hello Mika,
When dealing with a large number of files, it's important to avoid loading all the file names into memory at once. Instead, you can use a generator to iterate over the files one by one. This way, you only keep one file name in memory at a time.
Here's an example of how you can use a generator with Laravel's Filesystem to iterate over the files in a directory:
use Illuminate\Support\Facades\Storage;
function getFiles($disk, $directory) {
$allFiles = Storage::disk($disk)->allFiles($directory);
foreach ($allFiles as $file) {
yield $file;
}
}
$totalFiles = 0;
foreach (getFiles($disk, $directory) as $file) {
$totalFiles++;
}
echo "Total number of files: " . $totalFiles;
However, the above code might still cause an out of memory error because the allFiles method is used, which attempts to load all file names into an array.
To avoid this, you can use the files method in combination with a recursive function to process one directory at a time:
use Illuminate\Support\Facades\Storage;
function countFiles($disk, $directory) {
$totalFiles = 0;
$directories = [ $directory ];
while ($dir = array_shift($directories)) {
$files = Storage::disk($disk)->files($dir);
$totalFiles += count($files);
// Get subdirectories to process them later
$subdirectories = Storage::disk($disk)->directories($dir);
$directories = array_merge($directories, $subdirectories);
}
return $totalFiles;
}
$totalFiles = countFiles($disk, $directory);
echo "Total number of files: " . $totalFiles;
This code will count the files without loading them all into memory at once. It processes each directory and its files separately, and then moves on to the next directory.
Remember to adjust $disk and $directory to match your actual disk and directory names. Also, ensure that you have enough memory and execution time for the script to run, as iterating over a large number of files can be time-consuming.