@@ -167,6 +167,12 @@ public function remove($files)
167167 } elseif (!\is_array ($ files )) {
168168 $ files = [$ files ];
169169 }
170+
171+ self ::doRemove ($ files , false );
172+ }
173+
174+ private static function doRemove (array $ files , bool $ isRecursive ): void
175+ {
170176 $ files = array_reverse ($ files );
171177 foreach ($ files as $ file ) {
172178 if (is_link ($ file )) {
@@ -175,9 +181,26 @@ public function remove($files)
175181 throw new IOException (sprintf ('Failed to remove symlink "%s": ' , $ file ).self ::$ lastError );
176182 }
177183 } elseif (is_dir ($ file )) {
178- $ this ->remove (new \FilesystemIterator ($ file , \FilesystemIterator::CURRENT_AS_PATHNAME | \FilesystemIterator::SKIP_DOTS ));
184+ if (!$ isRecursive ) {
185+ $ file = realpath ($ file );
186+ $ tmpName = \dirname ($ file ).'/. ' .strrev (strtr (base64_encode (random_bytes (2 )), '/= ' , '-. ' ));
187+
188+ if (file_exists ($ tmpName )) {
189+ try {
190+ self ::doRemove ([$ tmpName ], true );
191+ } catch (IOException $ e ) {
192+ }
193+ }
194+
195+ if (!file_exists ($ tmpName ) && self ::box ('rename ' , $ file , $ tmpName )) {
196+ $ file = $ tmpName ;
197+ }
198+ }
199+
200+ $ files = new \FilesystemIterator ($ file , \FilesystemIterator::CURRENT_AS_PATHNAME | \FilesystemIterator::SKIP_DOTS );
201+ self ::doRemove (iterator_to_array ($ files , true ), true );
179202
180- if (!self ::box ('rmdir ' , $ file ) && file_exists ($ file )) {
203+ if (!self ::box ('rmdir ' , $ file ) && file_exists ($ file ) && ( $ isRecursive || $ file !== $ tmpName ) ) {
181204 throw new IOException (sprintf ('Failed to remove directory "%s": ' , $ file ).self ::$ lastError );
182205 }
183206 } elseif (!self ::box ('unlink ' , $ file ) && (false !== strpos (self ::$ lastError , 'Permission denied ' ) || file_exists ($ file ))) {
0 commit comments