Skip to content

Commit b1a9d58

Browse files
afiestasnicolas-grekas
authored andcommitted
[HttpFoundation] Handle error when directory is a file
In File::getTargetFile the directory is recursively created if it does not exists. If directory creation fails a FileException is thrown. This commit improves error clarity by adding a new Exception for the relatively common situation where the path exists but instead of a directory it is a file.
1 parent e088d76 commit b1a9d58

File tree

1 file changed

+4
-3
lines changed
  • src/Symfony/Component/HttpFoundation/File

1 file changed

+4
-3
lines changed

src/Symfony/Component/HttpFoundation/File/File.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,11 @@ public function getContent(): string
114114

115115
protected function getTargetFile(string $directory, ?string $name = null): self
116116
{
117-
if (!is_dir($directory)) {
118-
if (false === @mkdir($directory, 0o777, true) && !is_dir($directory)) {
119-
throw new FileException(\sprintf('Unable to create the "%s" directory.', $directory));
117+
if (!is_dir($directory) && !@mkdir($directory, 0o777, true) && !is_dir($directory)) {
118+
if (is_file($directory)) {
119+
throw new FileException(\sprintf('Unable to create the "%s" directory: a similarly-named file exists.', $directory));
120120
}
121+
throw new FileException(\sprintf('Unable to create the "%s" directory.', $directory));
121122
} elseif (!is_writable($directory)) {
122123
throw new FileException(\sprintf('Unable to write in the "%s" directory.', $directory));
123124
}

0 commit comments

Comments
 (0)