Created
May 25, 2017 20:43
-
-
Save rorymcdaniel/8d359ffe457de1dd7765bb7669446ba7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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