Please tell me how you can get to the array a list of all subfolders contained in the folder, so that the script does not fall off when you try to go to a folder to which it does not have read permissions, and also to keep working from php 5.2 to version 7.1.

  • can parsit issue exec (dir)? - Andrey Fedorov
  • No, I just needed a simple brick-like option that would work regardless of the version of PCP and the security settings of my resource. in any case, I received an answer to my question, thank you) - Nick

1 answer 1

$root = '/etc'; $iter = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($root, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST, // при блоке прав чтения не отвалится RecursiveIteratorIterator::CATCH_GET_CHILD // Ignore "Permission denied" (>>на которую у него нет прав на чтение) ); $paths = array($root); foreach ($iter as $path => $dir) { if ($dir->isDir()) { $paths[] = $path; } } print_r($paths); 

Result:

 Array ( [0] => /etc [1] => /etc/rc2.d [2] => /etc/luarocks ... [17] => /etc/php5 [18] => /etc/php5/apache2 [19] => /etc/php5/apache2/conf.d [20] => /etc/php5/mods-available [21] => /etc/php5/conf.d 

A source

You can safely use glob () works with (PHP 4> = 4.3.0, PHP 5, PHP 7). Here is a good example with glob .

 function glob_recursive($pattern, $flags = 0) { $files = glob($pattern, $flags); foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) { $files = array_merge($files, glob_recursive($dir.'/'.basename($pattern), $flags)); } return $files; } 

And another example .

  foreach($files as $key => $value){ $path = realpath($dir.DIRECTORY_SEPARATOR.$value); if(!is_dir($path)) { $results[] = $path; } else if($value != "." && $value != "..") { getDirContents($path, $results); $results[] = $path; } } return $results; } var_dump(getDirContents('/xampp/htdocs/WORK')); 

Result:

 array (size=12) 0 => string '/xampp/htdocs/WORK/iframe.html' (length=30) 1 => string '/xampp/htdocs/WORK/index.html' (length=29) 2 => string '/xampp/htdocs/WORK/js' (length=21) 3 => string '/xampp/htdocs/WORK/js/btwn.js' (length=29) 4 => string '/xampp/htdocs/WORK/js/qunit' (length=27) 

A source

  • Thank you very much, the link to the source of the first option is a good solution with the name of the expandDirectories function. I had to use array_unique, but in general the option came up. - Nick
  • @ Nick if the option approached you, mark it with the decision - DaemonHK