Skip to content

Instantly share code, notes, and snippets.

@greg-randall
Created December 12, 2022 15:11
Show Gist options
  • Select an option

  • Save greg-randall/2156825a4151e19ef8961a6197694d94 to your computer and use it in GitHub Desktop.

Select an option

Save greg-randall/2156825a4151e19ef8961a6197694d94 to your computer and use it in GitHub Desktop.
Search for a string in filename/foldernames using PHP
<?php
$search_string = ".bak";
$count = 0;
$output = "";
$root = '.';
$iter = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $root, RecursiveDirectoryIterator::SKIP_DOTS ), RecursiveIteratorIterator::SELF_FIRST, RecursiveIteratorIterator::CATCH_GET_CHILD );
$i=0;
foreach ( $iter as $path => $dir ) {
if( stripos($dir->__toString(),$search_string) !== false){
$output .= $dir->__toString() . "<br>";
$count++;
}
}
echo "<h3>Count: $count</h3><hr>$output";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment