From 447ff915df4dc896bb5e2f7dc069c9516ffe92d6 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 7 Dec 2012 10:25:02 +0100 Subject: [PATCH] [HttpFoundation] changed UploadedFile::move() to use move_uploaded_file() when possible (closes #5878, closes #6185) --- .../Component/HttpFoundation/File/File.php | 23 ++++++++++++------- .../HttpFoundation/File/UploadedFile.php | 17 ++++++++++++-- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/Symfony/Component/HttpFoundation/File/File.php b/src/Symfony/Component/HttpFoundation/File/File.php index 43b8c0d265..de4fb9d17b 100644 --- a/src/Symfony/Component/HttpFoundation/File/File.php +++ b/src/Symfony/Component/HttpFoundation/File/File.php @@ -523,6 +523,20 @@ class File extends \SplFileInfo * @api */ public function move($directory, $name = null) + { + $target = $this->getTargetFile($directory, $name); + + if (!@rename($this->getPathname(), $target)) { + $error = error_get_last(); + throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error['message']))); + } + + @chmod($target, 0666 & ~umask()); + + return $target; + } + + protected function getTargetFile($directory, $name = null) { if (!is_dir($directory)) { if (false === @mkdir($directory, 0777, true)) { @@ -534,14 +548,7 @@ class File extends \SplFileInfo $target = $directory.DIRECTORY_SEPARATOR.(null === $name ? $this->getBasename() : $this->getName($name)); - if (!@rename($this->getPathname(), $target)) { - $error = error_get_last(); - throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error['message']))); - } - - chmod($target, 0666); - - return new File($target); + return new File($target, false); } /** diff --git a/src/Symfony/Component/HttpFoundation/File/UploadedFile.php b/src/Symfony/Component/HttpFoundation/File/UploadedFile.php index 7b337da9ff..0830a8a06c 100644 --- a/src/Symfony/Component/HttpFoundation/File/UploadedFile.php +++ b/src/Symfony/Component/HttpFoundation/File/UploadedFile.php @@ -189,8 +189,21 @@ class UploadedFile extends File */ public function move($directory, $name = null) { - if ($this->isValid() && ($this->test || is_uploaded_file($this->getPathname()))) { - return parent::move($directory, $name); + if ($this->isValid()) { + if ($this->test) { + return parent::move($directory, $name); + } elseif (is_uploaded_file($this->getPathname())) { + $target = $this->getTargetFile($directory, $name); + + if (!@move_uploaded_file($this->getPathname(), $target)) { + $error = error_get_last(); + throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error['message']))); + } + + @chmod($target, 0666 & ~umask()); + + return $target; + } } throw new FileException(sprintf('The file "%s" has not been uploaded via Http', $this->getPathname()));