2

I want to get all folders and the number of files in them, so that these files can be displayed in a tree view for users. There are more files and I started getting an error "RecursiveDirectoryIterator::__construct(/storage/app/public/invoices/2835): failed to open dir: Too many open files".

How can I get around this error without using ajax, but only using php? I also have no way to increase the limit of open files

$aFiles = File::allFiles(storage_path('app/public/invoices/'.$supV->id));
                                
$name = [];

foreach ($aFiles as $aFile) {
      $name[] = array("name" => $aFile->getFilename());
      }

$fileCount = count($aFiles);

$invArray[$key]['date_del'][$k]['test'][$sup] = array("name" => $supV->name, "id" => $supV->id, "count" => $fileCount, "files" => $name);
  • If you're getting that error then perhaps you're not closing the resources you're opening when you don't need them anymore or your code is structured in such a way that you're forcing yourself to keep resources open – apokryfos Oct 12 '20 at 09:35
  • @apokryfos Yes, I understand this, but I do not know how to close unused resources – Alexander Schurf Oct 12 '20 at 09:41
  • Does this can help you ? https://stackoverflow.com/questions/12801370/count-how-many-files-in-directory-php Maybe it's an other (better ?) way about how to counter how many files there is in a directory – magrigry Oct 12 '20 at 09:43
  • Does this answer your question? [Count how many files in directory PHP](https://stackoverflow.com/questions/12801370/count-how-many-files-in-directory-php) – Alex Oct 12 '20 at 09:45
  • Ok in this case the issue is coming from the `allFiles` method rather than your code, you probably need to do a manual iteration of files in a directory. `allFiles` probably doesn't help you either since it's a flat listing of all files in folders and subfolders – apokryfos Oct 12 '20 at 10:16

0 Answers0