Skip to content

Instantly share code, notes, and snippets.

@solicomo
Last active December 8, 2016 09:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save solicomo/6522dde5b40761a2c2749c9ea5b185df to your computer and use it in GitHub Desktop.
Save solicomo/6522dde5b40761a2c2749c9ea5b185df to your computer and use it in GitHub Desktop.
[PHP] get all files in a path
<?php
function files_in($path, $types = 'f', $recursive = false)
{
$files = array();
//$path = realpath($path);
$dirs = new RecursiveDirectoryIterator($path);
$dirs->setFlags($dirs->getFlags() | FilesystemIterator::SKIP_DOTS);
if ($recursive) {
$dirs = new RecursiveIteratorIterator($dirs, RecursiveIteratorIterator::SELF_FIRST); // SELF_FIRST is important if you also want Dir children
}
foreach ($dirs as $name => $dir) {
if ($dir->isDir() && strpos($types, 'd') !== false) {
$files[] = $name;
}
if ($dir->isFile() && strpos($types, 'f') !== false) {
$files[] = $name;
}
}
return $files;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment