diff --git a/src/Symfony/Component/Form/FileField.php b/src/Symfony/Component/Form/FileField.php index 7941b8b602dea..593982d63ea71 100644 --- a/src/Symfony/Component/Form/FileField.php +++ b/src/Symfony/Component/Form/FileField.php @@ -13,6 +13,7 @@ use Symfony\Component\HttpFoundation\File\File; use Symfony\Component\Form\Exception\FormException; +use Symfony\Component\HttpFoundation\File\UploadedFile; /** * A file field to upload files. @@ -66,6 +67,9 @@ protected function configure() protected function preprocessData(array $data) { if ($data['file']) { + if (!$data['file'] instanceof UploadedFile) { + throw new \UnexpectedValueException('Uploaded file is not of type UploadedFile, your form tag is probably missing the enctype="multipart/form-data" attribute.'); + } switch ($data['file']->getError()) { case UPLOAD_ERR_INI_SIZE: $this->iniSizeExceeded = true; @@ -86,7 +90,7 @@ protected function preprocessData(array $data) default: $data['file']->move($this->getTmpDir()); $data['file']->rename($this->getTmpName($data['token'])); - $data['original_name'] = $data['file']->getOriginalName(); + $data['original_name'] = $data['file']->getName(); $data['file'] = ''; break; } diff --git a/src/Symfony/Component/HttpFoundation/File/File.php b/src/Symfony/Component/HttpFoundation/File/File.php index ea03ac4f0942b..d72405602852d 100644 --- a/src/Symfony/Component/HttpFoundation/File/File.php +++ b/src/Symfony/Component/HttpFoundation/File/File.php @@ -2,7 +2,7 @@ /* * This file is part of the Symfony package. - * + * * (c) Fabien Potencier * * For the full copyright and license information, please view the LICENSE @@ -497,7 +497,7 @@ public function __construct($path) */ public function __toString() { - return null === $this->getPath() ? '' : $this->getPath(); + return null === $this->path ? '' : $this->path; } /** diff --git a/src/Symfony/Component/HttpFoundation/File/UploadedFile.php b/src/Symfony/Component/HttpFoundation/File/UploadedFile.php index cf897885c936c..8386d73b6c149 100644 --- a/src/Symfony/Component/HttpFoundation/File/UploadedFile.php +++ b/src/Symfony/Component/HttpFoundation/File/UploadedFile.php @@ -2,7 +2,7 @@ /* * This file is part of the Symfony package. - * + * * (c) Fabien Potencier * * For the full copyright and license information, please view the LICENSE @@ -101,13 +101,18 @@ public function getMimeType() } /** - * Returns the original file name including its extension. + * Returns the absolute file name without dots + * + * Until the uploaded file is moved, it will return the name of the temporary file * - * @returns string The file name + * @returns string The file path */ - public function getOriginalName() + public function getName() { - return $this->originalName; + if (!$this->moved) { + return $this->originalName; + } + return parent::getName(); } /** diff --git a/tests/Symfony/Tests/Component/Form/FileFieldTest.php b/tests/Symfony/Tests/Component/Form/FileFieldTest.php index 4f0ecca9c5010..b6a44bbc98d4e 100644 --- a/tests/Symfony/Tests/Component/Form/FileFieldTest.php +++ b/tests/Symfony/Tests/Component/Form/FileFieldTest.php @@ -70,7 +70,7 @@ public function testSubmitUploadsNewFiles() $that->createTmpFile($tmpPath); })); $file->expects($this->any()) - ->method('getOriginalName') + ->method('getName') ->will($this->returnValue('original_name.jpg')); $this->field->submit(array( @@ -111,6 +111,18 @@ public function testSubmitKeepsUploadedFilesOnErrors() $this->assertEquals(realpath($tmpPath), realpath($this->field->getData())); } + /** + * @expectedException UnexpectedValueException + */ + public function testSubmitFailsOnMissingMultipart() + { + $this->field->submit(array( + 'file' => 'foo.jpg', + 'token' => '12345', + 'original_name' => 'original_name.jpg', + )); + } + public function testSubmitKeepsOldFileIfNotOverwritten() { $oldPath = tempnam(sys_get_temp_dir(), 'FileFieldTest'); diff --git a/tests/Symfony/Tests/Component/HttpFoundation/File/MimeType/MimeTypeTest.php b/tests/Symfony/Tests/Component/HttpFoundation/File/MimeType/MimeTypeTest.php index 56032dfd9ad29..389f93e040ad9 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/File/MimeType/MimeTypeTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/File/MimeType/MimeTypeTest.php @@ -59,6 +59,9 @@ public function testGuessWithIncorrectPath() public function testGuessWithNonReadablePath() { + if (strstr(PHP_OS, 'WIN')) { + $this->markTestSkipped('Can not verify chmod operations on Windows'); + } $path = __DIR__.'/../Fixtures/to_delete'; touch($path); chmod($path, 0333); diff --git a/tests/Symfony/Tests/Component/HttpFoundation/File/UploadedFileTest.php b/tests/Symfony/Tests/Component/HttpFoundation/File/UploadedFileTest.php index a1e3249a150d8..bc924f2bb6481 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/File/UploadedFileTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/File/UploadedFileTest.php @@ -95,7 +95,7 @@ public function testGetOriginalName() null ); - $this->assertEquals('original.gif', $file->getOriginalName()); + $this->assertEquals('original.gif', $file->getName()); } } } \ No newline at end of file