This snippet is fairly specific to the task it was required for, a client had a directory on their website that contained literally thousands of folders inside it, not nested just a ton of folders inside of one folder. The task was to search each one of these sub-folders (not recursively to any sub-folders inside these first level ones, just the first level children to the target directory) and search for a file called ‘index.php’ and delete it (the client preferred another file extension be used for the index page).
Think…
-target_directory
| -subfolder_1
| -index.html
| -index.php
| -subfolder_2
| -index.html
| -index.php
| -subfolder_n
| -index.html
| -index.php
// open the target directory
$dh = opendir('/path/to/target/directory');
// iterate through the folders in the target directory
while ( ( $file = readdir($dh) ) !== false )
{
// only check folders, exclude files and the directory controls
if ( $file != "." && $file != ".." && !is_file($file) )
{
$index_path = $path.'/'.$file.'/'.'index.php';
// check if 'index.php' exists
if ( is_file($index_path) )
{
// delete 'index.php'
unlink($index_path);
}
}
}
closedir($dh);