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

Skip to content

[HttpFoundation] changed UploadedFile::move() to use move_uploaded_file() when possible (closes #5878, closes #6185) #6221

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

Merged
merged 1 commit into from
Dec 7, 2012
Merged
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 @@ -523,6 +523,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 @@ -534,14 +548,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);

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

/**
Expand Down
17 changes: 15 additions & 2 deletions src/Symfony/Component/HttpFoundation/File/UploadedFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,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