Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* Recursively searches the current directory and deletes any file that has only an opening PHP tag and empty lines
* Thanks to Jan for providing the solution here:
* https://stackoverflow.com/questions/44185401/delete-file-containing-opening-php-tag-and-empty-lines
*/
$directory = new RecursiveDirectoryIterator('./');
$iterator = new RecursiveIteratorIterator($directory);
$regex = '~\A\Q<?php\E\s+\Q\E\Z~';
$deletedCount = 0;
foreach($iterator as $name => $file) {
$content = file_get_contents($file);
if (preg_match($regex, $content)) {
print "deleted " . $name . "\n";
unlink($file);
$deletedCount++;
}
}
print "Deleted $deletedCount empty files.\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.