bug #18515 [Filesystem] Better error handling in remove() (nicolas-grekas)
This PR was submitted for the master branch but it was merged into the 2.3 branch instead (closes #18515).
Discussion
----------
[Filesystem] Better error handling in remove()
| Q | A
| ------------- | ---
| Branch? | master (to be moved on 2.3 when merging)
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | #18512
| License | MIT
| Doc PR | -
Commits
-------
b848ddb
[Filesystem] Better error handling in remove()
This commit is contained in:
commit
6401371c7e
|
@ -140,24 +140,27 @@ class Filesystem
|
|||
*/
|
||||
public function remove($files)
|
||||
{
|
||||
$files = iterator_to_array($this->toIterator($files));
|
||||
if ($files instanceof \Traversable) {
|
||||
$files = iterator_to_array($files, false);
|
||||
} elseif (!is_array($files)) {
|
||||
$files = array($files);
|
||||
}
|
||||
$files = array_reverse($files);
|
||||
foreach ($files as $file) {
|
||||
if (@(unlink($file) || rmdir($file))) {
|
||||
continue;
|
||||
}
|
||||
if (is_link($file)) {
|
||||
// See https://bugs.php.net/52176
|
||||
$error = error_get_last();
|
||||
throw new IOException(sprintf('Failed to remove symlink "%s": %s.', $file, $error['message']));
|
||||
if (!@(unlink($file) || '\\' !== DIRECTORY_SEPARATOR || rmdir($file)) && file_exists($file)) {
|
||||
$error = error_get_last();
|
||||
throw new IOException(sprintf('Failed to remove symlink "%s": %s.', $file, $error['message']));
|
||||
}
|
||||
} elseif (is_dir($file)) {
|
||||
$this->remove(new \FilesystemIterator($file));
|
||||
$this->remove(new \FilesystemIterator($file, \FilesystemIterator::CURRENT_AS_PATHNAME | \FilesystemIterator::SKIP_DOTS));
|
||||
|
||||
if (!@rmdir($file)) {
|
||||
if (!@rmdir($file) && file_exists($file)) {
|
||||
$error = error_get_last();
|
||||
throw new IOException(sprintf('Failed to remove directory "%s": %s.', $file, $error['message']));
|
||||
}
|
||||
} elseif (file_exists($file)) {
|
||||
} elseif (!@unlink($file) && file_exists($file)) {
|
||||
$error = error_get_last();
|
||||
throw new IOException(sprintf('Failed to remove file "%s": %s.', $file, $error['message']));
|
||||
}
|
||||
|
|
Reference in New Issue