@@ -167,6 +167,12 @@ public function remove($files)
167
167
} elseif (!\is_array ($ files )) {
168
168
$ files = [$ files ];
169
169
}
170
+
171
+ self ::doRemove ($ files , false );
172
+ }
173
+
174
+ private static function doRemove (array $ files , bool $ isRecursive ): void
175
+ {
170
176
$ files = array_reverse ($ files );
171
177
foreach ($ files as $ file ) {
172
178
if (is_link ($ file )) {
@@ -175,9 +181,26 @@ public function remove($files)
175
181
throw new IOException (sprintf ('Failed to remove symlink "%s": ' , $ file ).self ::$ lastError );
176
182
}
177
183
} 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 );
179
202
180
- if (!self ::box ('rmdir ' , $ file ) && file_exists ($ file )) {
203
+ if (!self ::box ('rmdir ' , $ file ) && file_exists ($ file ) && ( $ isRecursive || $ file !== $ tmpName ) ) {
181
204
throw new IOException (sprintf ('Failed to remove directory "%s": ' , $ file ).self ::$ lastError );
182
205
}
183
206
} elseif (!self ::box ('unlink ' , $ file ) && (false !== strpos (self ::$ lastError , 'Permission denied ' ) || file_exists ($ file ))) {
0 commit comments