fixed file upload

This commit is contained in:
Fabien Potencier 2010-09-09 16:07:12 +02:00
parent 40c0fe854f
commit fc9325a737
3 changed files with 10 additions and 4 deletions

View File

@ -466,7 +466,7 @@ class File
*/ */
public function __toString() public function __toString()
{ {
return $this->getPath(); return null === $this->getPath() ? '' : $this->getPath();
} }
/** /**

View File

@ -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'))); 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)) { if (is_null($error)) {
$error = UPLOAD_ERR_OK; $error = UPLOAD_ERR_OK;

View File

@ -20,6 +20,10 @@ class FileValidator extends ConstraintValidator
throw new UnexpectedTypeException($value, 'string'); throw new UnexpectedTypeException($value, 'string');
} }
if ($value instanceof FileObject && null === $value->getPath()) {
return true;
}
$path = $value instanceof FileObject ? $value->getPath() : (string)$value; $path = $value instanceof FileObject ? $value->getPath() : (string)$value;
if (!file_exists($path)) { if (!file_exists($path)) {
@ -39,11 +43,11 @@ class FileValidator extends ConstraintValidator
$size = filesize($path); $size = filesize($path);
$limit = $constraint->maxSize; $limit = $constraint->maxSize;
$suffix = ' bytes'; $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); $size = round(filesize($path) / 1000, 2);
$limit = $matches[1]; $limit = $matches[1];
$suffix = ' kB'; $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); $size = round(filesize($path) / 1000000, 2);
$limit = $matches[1]; $limit = $matches[1];
$suffix = ' MB'; $suffix = ' MB';