Skip to content

Instantly share code, notes, and snippets.

@useless-stuff
Last active February 20, 2016 09:09
Show Gist options
  • Save useless-stuff/b77bbae02233250cc60a to your computer and use it in GitHub Desktop.
Save useless-stuff/b77bbae02233250cc60a to your computer and use it in GitHub Desktop.
PHP - RecursiveDirectoryIterator
<?php
$filePath = '/vagrant/data/anniballo.com';
/**
* Class FolderFilter
*/
class DotFilter extends RecursiveFilterIterator
{
/**
* @return bool
*/
public function accept()
{
return !$this->getInnerIterator()->isDot();
}
}
$filter = new DotFilter(new RecursiveDirectoryIterator($filePath));
function scan($iterator,$level = null){
foreach($iterator as $index => $value){
if($level){
$value = basename($value);
}
echo ($iterator->hasChildren()?'[D] ':'[F] '),$level, $value, PHP_EOL;
if($iterator->hasChildren()){
scan($iterator->getChildren(),$level."--> ");
}
}
}
scan($filter);
// Output:
/*
[...]
[F] /vagrant/data/anniballo.com/wp-blog-header.php
[F] /vagrant/data/anniballo.com/wp-comments-post.php
[F] /vagrant/data/anniballo.com/wp-config-sample.php
[F] /vagrant/data/anniballo.com/wp-config.php
[D] /vagrant/data/anniballo.com/wp-content
[F] --> .DS_Store
[F] --> index.php
[D] --> languages
[F] --> --> admin-it_IT.mo
[F] --> --> admin-it_IT.po
[F] --> --> admin-network-it_IT.mo
[F] --> --> admin-network-it_IT.po
[F] --> --> continents-cities-it_IT.mo
[F] --> --> continents-cities-it_IT.po
[F] --> --> it_IT.mo
[F] --> --> it_IT.po
[D] --> --> plugins
[F] --> --> --> akismet-it_IT.mo
[F] --> --> --> akismet-it_IT.po
[D] --> --> themes
[F] --> --> --> twentyfifteen-it_IT.mo
[F] --> --> --> twentyfifteen-it_IT.po
[F] --> --> --> twentyfourteen-it_IT.mo
[F] --> --> --> twentyfourteen-it_IT.po
[F] --> --> --> twentysixteen-it_IT.mo
[F] --> --> --> twentysixteen-it_IT.po
[D] --> plugins
[F] --> --> .DS_Store
[D] --> --> akismet
[F] --> --> --> .htaccess
[...]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment