8000 [HttpFoundation] changed UploadedFile::move() to use move_uploaded_fi… · norberttech/symfony@447ff91 · GitHub
[go: up one dir, main page]

Skip to content

Commit 447ff91

Browse files
committed
[HttpFoundation] changed UploadedFile::move() to use move_uploaded_file() when possible (closes symfony#5878, closes symfony#6185)
1 parent e277258 commit 447ff91

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,20 @@ public function getExtension()
523523
* @api
524524
*/
525525
public function move($directory, $name = null)
526+
{
527+
$target = $this->getTargetFile($directory, $name);
528+
529+
if (!@rename($this->getPathname(), $target)) {
530+
$error = error_get_last();
531+
throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error['message'])));
532+
}
533+
534+
@chmod($target, 0666 & ~umask());
535+
536+
return $target;
537+
}
538+
539+
protected function getTargetFile($directory, $name = null)
526540
{
527541
if (!is_dir($directory)) {
528542
if (false === @mkdir($directory, 0777, true)) {
@@ -534,14 +548,7 @@ public function move($directory, $name = null)
534548

535549
$target = $directory.DIRECTORY_SEPARATOR.(null === $name ? $this->getBasename() : $this->getName($name));
536550

537-
if (!@rename($this->getPathname(), $target)) {
538-
$error = error_get_last();
539-
throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error['message'])));
540-
}
541-
542-
chmod($target, 0666);
543-
544-
return new File($target);
551+
return new File($target, false);
545552
}
546553

547554
/**

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,21 @@ public function isValid()
189189
*/
190190
public function move($directory, $name = null)
191191
{
192-
if ($this->isValid() && ($this->test || is_uploaded_file($this->getPathname()))) {
193-
return parent::move($directory, $name);
192+
if ($this->isValid()) {
193+
if ($this->test) {
194+
return parent::move($directory, $name);
195+
} elseif (is_uploaded_file($this->getPathname())) {
196+
$target = $this->getTargetFile($directory, $name);
197+
198+
if (!@move_uploaded_file($this->getPathname(), $target)) {
199+
$error = error_get_last();
200+
throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error['message'])));
201+
}
202+
203+
@chmod($target, 0666 & ~umask());
204+
205+
return $target;
206+
}
194207
}
195208

196209
throw new FileException(sprintf('The file "%s" has not been uploaded via Http', $this->getPathname()));

0 commit comments

Comments
 (0)
0