8000 [HttpFoundation] changed UploadedFile::move() to use move_uploaded_file() when possible by fabpot · Pull Request #6211 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[HttpFoundation] changed UploadedFile::move() to use move_uploaded_file() when possible #6211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/Symfony/Component/HttpFoundation/File/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,20 @@ public function getExtension()
* @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)) {
Expand All @@ -117,14 +131,7 @@ public function move($directory, $name = null)

$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 & ~umask());

return new File($target);
return new File($target, false);
}

/**
Expand Down
17 changes: 15 additions & 2 deletions src/Symfony/Component/HttpFoundation/File/UploadedFile.php
45C7
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,21 @@ public function isValid()
*/
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()));
Expand Down
0