Skip to content

Commit

Permalink
fixed file upload
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Sep 9, 2010
1 parent 40c0fe8 commit fc9325a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Symfony/Component/File/File.php
Expand Up @@ -466,7 +466,7 @@ public function __construct($path)
*/
public function __toString()
{
return $this->getPath();
return null === $this->getPath() ? '' : $this->getPath();
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Component/File/UploadedFile.php
Expand Up @@ -43,7 +43,9 @@ public function __construct($path, $originalName, $mimeType, $size, $error)
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;
Expand Down
8 changes: 6 additions & 2 deletions src/Symfony/Component/Validator/Constraints/FileValidator.php
Expand Up @@ -20,6 +20,10 @@ public function isValid($value, Constraint $constraint)
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)) {
Expand All @@ -39,11 +43,11 @@ public function isValid($value, Constraint $constraint)
$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';
Expand Down

0 comments on commit fc9325a

Please sign in to comment.