Skip to content

Instantly share code, notes, and snippets.

@rorymcdaniel
Created May 25, 2017 20:43
Show Gist options
  • Save rorymcdaniel/8d359ffe457de1dd7765bb7669446ba7 to your computer and use it in GitHub Desktop.
Save rorymcdaniel/8d359ffe457de1dd7765bb7669446ba7 to your computer and use it in GitHub Desktop.
<?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