diff --git a/src/Symfony/Component/Validator/Constraints/FileValidator.php b/src/Symfony/Component/Validator/Constraints/FileValidator.php index e2b97b03ae204..e4e02c80fd18b 100644 --- a/src/Symfony/Component/Validator/Constraints/FileValidator.php +++ b/src/Symfony/Component/Validator/Constraints/FileValidator.php @@ -171,7 +171,7 @@ public function validate(mixed $value, Constraint $constraint) $mimeTypes = (array) $constraint->mimeTypes; if ($constraint->extensions) { - $fileExtension = pathinfo($path, \PATHINFO_EXTENSION); + $fileExtension = pathinfo($basename, \PATHINFO_EXTENSION); $found = false; $normalizedExtensions = []; diff --git a/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php index 1fd0c3857967d..c749480b87e49 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php @@ -640,5 +640,22 @@ public function testExtensionTypesIncoherent() ->assertRaised(); } + public function testUploadedFileExtensions() + { + $file = new UploadedFile(__DIR__.'/Fixtures/bar', 'bar.txt', 'text/plain', \UPLOAD_ERR_OK, true); + + try { + $file->getMimeType(); + } catch (\LogicException $e) { + $this->markTestSkipped('Guessing the mime type is not possible'); + } + + $constraint = new File(mimeTypesMessage: 'myMessage', extensions: ['txt']); + + $this->validator->validate($file, $constraint); + + $this->assertNoViolation(); + } + abstract protected function getFile($filename); }