Has anyone encountered a problem when scandir() does not return the entire contents of a directory? In my case, the directory contains 25,000 directories, but scandir() returns 10,590 . The rights of the directories are the same, they are not hidden, they open freely via FileZilla. What could be the problem?

 $partnumbers = scandir($pathdir); var_dump(count($partnumbers));die; 
  • Perhaps affected by the time execution of the script - Vladimir
  • one
    What OS? Is SELinux enabled? - AntonioK

2 answers 2

UPD 2

Check if SELinux is enabled, most likely it limits. He may even root. The admin at work says that he always turns off SELinux completely.

Perhaps there are some limits. In general, it is not very good to have such a number of directories in one directory. Check again right, most likely the problem is still in them.

In PHP, it is better to use iterators , they should work faster than procedures with a large number of cycles. For example:

 $path = realpath('/etc'); $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST); foreach($objects as $name => $object){ echo "$name\n"; } 

UPD

You can also use generators, by the way, they seem to be faster than iterators even:

 function getAllFiles($dirName) { $dh = opendir($dirName); while (false !== ($fileName = readdir($dh))) { yield $fileName; } closedir($dh); } foreach (getAllFiles('/etc') as $fileName) { echo "$fileName\n"; } 

Apruv on speed work

Test for 70 thousand files on 1Kb and 40 thousand directories in one directory.

scandir () , yield , DirectoryIterator

scandir () eats RAM, this is understandable, it is necessary to store a place for an array of data. Speed generators did not add, sadness. But the DirectoryIterator iterator won both in speed and RAM consumption.

"On matches" of course, but with very large volumes I think it will be noticeable.

  • DirectoryIterator issues the same number of directories - LANSELOT
  • @LANSELOT means the problem is exactly in the rights - korytoff
  • @LANSELOT Try typing ls -l | wc -l in console ls -l | wc -l ls -l | wc -l to find out the real number of files - korytoff
  • @LANSELOT and you can still see how many files with the rights of a particular user or group are - ls -l | grep "root" | wc -l ls -l | grep "root" | wc -l ls -l | grep "root" | wc -l (where root needs to be replaced with its user web server) - korytoff
  • ls -l | wc -l displays as expected, ls -l | grep "root" | wc -l too - LANSELOT

scandir only returns files / folders in the current path. It does not bypass nested directories recursively.

  • They are all in the same directory, recursiveness is not needed - LANSELOT