Skip to content

Instantly share code, notes, and snippets.

@EvilWolf
Created January 6, 2018 15:39
Show Gist options
  • Save EvilWolf/59aa13c51ce1c557c67ad8e3bd4d725d to your computer and use it in GitHub Desktop.
Save EvilWolf/59aa13c51ce1c557c67ad8e3bd4d725d to your computer and use it in GitHub Desktop.
Get all files form directory recursive | php generator
<?php
/* Get all and recursive files list, generator */
function genDirectoryRecursive($path) {
$directoryIterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($path),
RecursiveIteratorIterator::CHILD_FIRST,
RecursiveIteratorIterator::CATCH_GET_CHILD
);
foreach ($directoryIterator as $fileEntity) {
$file = [
'fileName' => $fileEntity->getFilename(),
'realPath' => $fileEntity->getRealPath(),
'hash' => hash_file('sha1', $fileEntity->getRealPath()),
'mtime' => $fileEntity->getMTime(),
'isDir' => $fileEntity->isDir()
];
/* Skip 'dot' folders */
if ($file['fileName'] === '.' OR $file['fileName'] === '..') {
continue;
}
yield $file;
}
}
foreach (genDirectoryRecursive('/var/www/') as $file) {
var_dump($file);
}
?>
/** Result **/
php genDirectoryRecursive.php
array(5) {
["fileName"]=>
string(23) "index.nginx-debian.html"
["realPath"]=>
string(37) "/var/www/html/index.nginx-debian.html"
["hash"]=>
string(40) "34fd73fa32c78a59f97b17fa4c173fe5eb025d0e"
["mtime"]=>
int(1479334897)
["isDir"]=>
bool(false)
}
array(5) {
["fileName"]=>
string(10) "index.html"
["realPath"]=>
string(24) "/var/www/html/index.html"
["hash"]=>
string(40) "d23f3a5389aee902652b149cbe2474a12c57fa5a"
["mtime"]=>
int(1479334883)
["isDir"]=>
bool(false)
}
array(5) {
["fileName"]=>
string(4) "html"
["realPath"]=>
string(13) "/var/www/html"
["hash"]=>
string(40) "da39a3ee5e6b4b0d3255bfef95601890afd80709"
["mtime"]=>
int(1479334897)
["isDir"]=>
bool(true)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment