From 3d653547684c08fc07cde75bbdd875ab74519fae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=80lex=20Fiestas?= Date: Tue, 17 Jun 2025 11:11:38 +0200 Subject: [PATCH] [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. --- src/Symfony/Component/HttpFoundation/File/File.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Symfony/Component/HttpFoundation/File/File.php b/src/Symfony/Component/HttpFoundation/File/File.php index 2194c178aee7e..a94fd93c5f73e 100644 --- a/src/Symfony/Component/HttpFoundation/File/File.php +++ b/src/Symfony/Component/HttpFoundation/File/File.php @@ -114,6 +114,10 @@ public function getContent(): string protected function getTargetFile(string $directory, ?string $name = null): self { + if (is_file($directory)) { + throw new FileException(\sprintf('The path "%s" is a file and it was expected to be a directory.', $directory)); + } + if (!is_dir($directory)) { if (false === @mkdir($directory, 0777, true) && !is_dir($directory)) { throw new FileException(\sprintf('Unable to create the "%s" directory.', $directory));