From 7562dc56f831b1129e142751b035104c7b15475b Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Tue, 13 Sep 2016 15:23:11 -0700 Subject: [PATCH] Ensure the entire uploads directory is empty Builds an array of files and directories to be removed, then removes them. This ensures `RecursiveIteratorIterator` internal index isn't messed up as we remove files. --- php/commands/site.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/php/commands/site.php b/php/commands/site.php index f9d9d9aa90..3fad434913 100644 --- a/php/commands/site.php +++ b/php/commands/site.php @@ -188,14 +188,24 @@ public function _empty( $args, $assoc_args ) { RecursiveIteratorIterator::CHILD_FIRST ); + $files_to_unlink = $directories_to_delete = array(); foreach ( $files as $fileinfo ) { $realpath = $fileinfo->getRealPath(); // Don't clobber subsites when operating on the main site if ( is_main_site() && false !== stripos( $realpath, '/sites/' ) ) { continue; } - $todo = $fileinfo->isDir() ? 'rmdir' : 'unlink'; - $todo( $realpath ); + if ( $fileinfo->isDir() ) { + $directories_to_delete[] = $realpath; + } else { + $files_to_unlink[] = $realpath; + } + } + foreach( $files_to_unlink as $file ) { + unlink( $file ); + } + foreach( $directories_to_delete as $directory ) { + rmdir( $directory ); } rmdir( $upload_dir['basedir'] ); }