@@ -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,10 +181,35 @@ 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
+ $ tmpName = \dirname (realpath ($ file )).'/. ' .strrev (strtr (base64_encode (random_bytes (2 )), '/= ' , '-. ' ));
186
+
187
+ if (file_exists ($ tmpName )) {
188
+ try {
189
+ self ::doRemove ([$ tmpName ], true );
190
+ } catch (IOException $ e ) {
191
+ }
192
+ }
193
+
194
+ if (!file_exists ($ tmpName ) && self ::box ('rename ' , $ file , $ tmpName )) {
195
+ $ origFile = $ file ;
196
+ $ file = $ tmpName ;
197
+ } else {
198
+ $ origFile = null ;
199
+ }
200
+ }
201
+
202
+ $ files = new \FilesystemIterator ($ file , \FilesystemIterator::CURRENT_AS_PATHNAME | \FilesystemIterator::SKIP_DOTS );
203
+ self ::doRemove (iterator_to_array ($ files , true ), true );
204
+
205
+ if (!self ::box ('rmdir ' , $ file ) && file_exists ($ file ) && !$ isRecursive ) {
206
+ $ lastError = self ::$ lastError ;
207
+
208
+ if (null !== $ origFile && self ::box ('rename ' , $ file , $ origFile )) {
209
+ $ file = $ origFile ;
210
+ }
179
211
180
- if (!self ::box ('rmdir ' , $ file ) && file_exists ($ file )) {
181
- throw new IOException (sprintf ('Failed to remove directory "%s": ' , $ file ).self ::$ lastError );
212
+ throw new IOException (sprintf ('Failed to remove directory "%s": ' , $ file ).$ lastError );
182
213
}
183
214
} elseif (!self ::box ('unlink ' , $ file ) && (false !== strpos (self ::$ lastError , 'Permission denied ' ) || file_exists ($ file ))) {
184
215
throw new IOException (sprintf ('Failed to remove file "%s": ' , $ file ).self ::$ lastError );
0 commit comments