123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- <?php
- function deleteTree(
- $folder,
- $keepRootFolder = false
- ) {
-
- if (empty($folder) || !file_exists($folder)) {
- return true;
- } elseif (is_file($folder) || is_link($folder)) {
- return @unlink($folder);
- }
-
- $files = new RecursiveIteratorIterator(
- new RecursiveDirectoryIterator($folder, RecursiveDirectoryIterator::SKIP_DOTS),
- RecursiveIteratorIterator::CHILD_FIRST
- );
- foreach ($files as $fileinfo) {
- $action = ($fileinfo->isDir() ? 'rmdir' : 'unlink');
- if (!@$action($fileinfo->getRealPath())) {
- return false;
- }
- }
-
- return (!$keepRootFolder ? @rmdir($folder) : true);
- }
|