[HttpFoundation] changed UploadedFile::move() to use move_uploaded_file() when possible (closes #5878, closes #6185)
This commit is contained in:
parent
e277258e91
commit
447ff915df
@ -523,6 +523,20 @@ class File extends \SplFileInfo
|
|||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
public function move($directory, $name = null)
|
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 (!is_dir($directory)) {
|
||||||
if (false === @mkdir($directory, 0777, true)) {
|
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));
|
$target = $directory.DIRECTORY_SEPARATOR.(null === $name ? $this->getBasename() : $this->getName($name));
|
||||||
|
|
||||||
if (!@rename($this->getPathname(), $target)) {
|
return new File($target, false);
|
||||||
$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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -189,8 +189,21 @@ class UploadedFile extends File
|
|||||||
*/
|
*/
|
||||||
public function move($directory, $name = null)
|
public function move($directory, $name = null)
|
||||||
{
|
{
|
||||||
if ($this->isValid() && ($this->test || is_uploaded_file($this->getPathname()))) {
|
if ($this->isValid()) {
|
||||||
return parent::move($directory, $name);
|
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()));
|
throw new FileException(sprintf('The file "%s" has not been uploaded via Http', $this->getPathname()));
|
||||||
|
Reference in New Issue
Block a user