From fc9325a737f0c9764b2315484ef59646f841bf58 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 9 Sep 2010 16:07:12 +0200 Subject: [PATCH] fixed file upload --- src/Symfony/Component/File/File.php | 2 +- src/Symfony/Component/File/UploadedFile.php | 4 +++- .../Component/Validator/Constraints/FileValidator.php | 8 ++++++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/File/File.php b/src/Symfony/Component/File/File.php index 4d9f4c76ca..4d955a02af 100644 --- a/src/Symfony/Component/File/File.php +++ b/src/Symfony/Component/File/File.php @@ -466,7 +466,7 @@ class File */ public function __toString() { - return $this->getPath(); + return null === $this->getPath() ? '' : $this->getPath(); } /** diff --git a/src/Symfony/Component/File/UploadedFile.php b/src/Symfony/Component/File/UploadedFile.php index 48f495ccd0..1482977a71 100644 --- a/src/Symfony/Component/File/UploadedFile.php +++ b/src/Symfony/Component/File/UploadedFile.php @@ -43,7 +43,9 @@ class UploadedFile extends File throw new FileException(sprintf('Unable to create UploadedFile because "file_uploads" is disabled in your php.ini file (%s)', get_cfg_var('cfg_file_path'))); } - parent::__construct($path); + if (is_file($path)) { + $this->path = realpath($path); + } if (is_null($error)) { $error = UPLOAD_ERR_OK; diff --git a/src/Symfony/Component/Validator/Constraints/FileValidator.php b/src/Symfony/Component/Validator/Constraints/FileValidator.php index 0ac61558f9..5651e75d91 100644 --- a/src/Symfony/Component/Validator/Constraints/FileValidator.php +++ b/src/Symfony/Component/Validator/Constraints/FileValidator.php @@ -20,6 +20,10 @@ class FileValidator extends ConstraintValidator throw new UnexpectedTypeException($value, 'string'); } + if ($value instanceof FileObject && null === $value->getPath()) { + return true; + } + $path = $value instanceof FileObject ? $value->getPath() : (string)$value; if (!file_exists($path)) { @@ -39,11 +43,11 @@ class FileValidator extends ConstraintValidator $size = filesize($path); $limit = $constraint->maxSize; $suffix = ' bytes'; - } else if (preg_match('/^(\d)k$/', $constraint->maxSize, $matches)) { + } else if (preg_match('/^(\d+)k$/', $constraint->maxSize, $matches)) { $size = round(filesize($path) / 1000, 2); $limit = $matches[1]; $suffix = ' kB'; - } else if (preg_match('/^(\d)M$/', $constraint->maxSize, $matches)) { + } else if (preg_match('/^(\d+)M$/', $constraint->maxSize, $matches)) { $size = round(filesize($path) / 1000000, 2); $limit = $matches[1]; $suffix = ' MB';