note 127006 added to class.recursivedirectoryiterator

From: Date: Sat, 23 Apr 2022 20:29:30 +0000
Subject: note 127006 added to class.recursivedirectoryiterator
Groups: php.notes 
Request: Send a blank email to php-notes+get-218307@lists.php.net to get a copy of this message
if you just want a plain array, recursively, of all files (not directories), try <?php /** * returns a plain string array of path to all files in a directory, and subdirectories, * but does not return directories themselves. (meaning if a directory is empty, it will not be included at all) * * @param string $dir * @param bool $realpath * @throws UnexpectedValueException if $dir is not readable/does not exists * @return string[] files */ function get_file_list_recursively(string $dir, bool $realpath = false): array { $files = array(); $files = []; foreach ((new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS))) as $file) { /** @var SplFileInfo $file */ if ($realpath) { $files[] = $file->getRealPath(); } else { $files[] = $file->getPathname(); } } return $files; } ?> sample return: <?php array ( 0 => '/home/hans/scraping/1650518081RESULTS.txt', 1 => '/home/hans/scraping/1650518121RESULTS.txt', 2 => '/home/hans/scraping/1650518679RESULTS.txt', 3 => '/home/hans/scraping/1650518780RESULTS.txt', 4 => '/home/hans/scraping/1650522198RESULTS.txt', 5 => '/home/hans/scraping/1650522927RESULTS.txt', 6 => '/home/hans/scraping/1650525391RESULTS.txt', 7 => '/home/hans/scraping/check_cache.php', 8 => '/home/hans/scraping/foo/bar.txt', 9 => '/home/hans/scraping/foobar.txt', 10 => '/home/hans/scraping/GoodProxyDaemon.php', ); ?> (note to editor, if you can find a better sample return, feel absolutely free to overwrite the one above) ---- Server IP: 45.112.84.4 Probable Submitter: 80.90.5.135 (proxied: 84.49.158.188) ---- Manual Page -- https://php.net/manual/en/class.recursivedirectoryiterator.php Edit -- https://main.php.net/note/edit/127006 Del: integrated -- https://main.php.net/note/delete/127006/integrated Del: useless -- https://main.php.net/note/delete/127006/useless Del: bad code -- https://main.php.net/note/delete/127006/bad+code Del: spam -- https://main.php.net/note/delete/127006/spam Del: non-english -- https://main.php.net/note/delete/127006/non-english Del: in docs -- https://main.php.net/note/delete/127006/in+docs Del: other reasons-- https://main.php.net/note/delete/127006 Reject -- https://main.php.net/note/reject/127006 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#218307) next »