Created
February 24, 2016 20:49
-
-
Save settermjd/66e571c76714c5364891 to your computer and use it in GitHub Desktop.
A simple script to recursively delete all files and directories within a parent directory
This file contains hidden or 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 | |
/** | |
* @param string $directory | |
*/ | |
function emptyDirectoryContents($directory) | |
{ | |
$Iterator = new RecursiveIteratorIterator( | |
new \RecursiveDirectoryIterator( | |
realpath($directory), | |
FilesystemIterator::SKIP_DOTS | |
), RecursiveIteratorIterator::CHILD_FIRST | |
); | |
/** @var \SplFileInfo $file */ | |
foreach ($Iterator as $file) { | |
if ($file->isDir()) { | |
rmdir($file->getRealPath()); | |
} else { | |
unlink($file->getRealPath()); | |
} | |
} | |
} |
rmdir
deletes only empty directories.
thanks @SenseException. It's an evolving work in progress. Should be finished a first version by the end of tomorrow.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Need to wrap the
RecursiveIteratorIterator
in aRegexIterator
or something similar. That way I can be sure I'm only deleting the files and directories which are required, not anything and everything, such as.gitkeep
files etc.