FileValidator.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Validator\Constraints;
  11. use Symfony\Component\Validator\Constraint;
  12. use Symfony\Component\Validator\ConstraintValidator;
  13. use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
  14. use Symfony\Component\Validator\Exception\UnexpectedTypeException;
  15. use Symfony\Component\HttpFoundation\File\File as FileObject;
  16. use Symfony\Component\HttpFoundation\File\UploadedFile;
  17. class FileValidator extends ConstraintValidator
  18. {
  19. public function isValid($value, Constraint $constraint)
  20. {
  21. if (null === $value || '' === $value) {
  22. return true;
  23. }
  24. if ($value instanceof UploadedFile && !$value->isValid()) {
  25. switch ($value->getError()) {
  26. case UPLOAD_ERR_INI_SIZE:
  27. $maxSize = UploadedFile::getMaxFilesize();
  28. $maxSize = $constraint->maxSize ? min($maxSize, $constraint->maxSize) : $maxSize;
  29. $this->setMessage($constraint->uploadIniSizeErrorMessage, array('{{ limit }}' => $maxSize.' bytes'));
  30. return false;
  31. case UPLOAD_ERR_FORM_SIZE:
  32. $this->setMessage($constraint->uploadFormSizeErrorMessage);
  33. return false;
  34. default:
  35. $this->setMessage($constraint->uploadErrorMessage);
  36. return false;
  37. }
  38. }
  39. if (!is_scalar($value) && !$value instanceof FileObject && !(is_object($value) && method_exists($value, '__toString'))) {
  40. throw new UnexpectedTypeException($value, 'string');
  41. }
  42. $path = $value instanceof FileObject ? $value->getPathname() : (string) $value;
  43. if (!file_exists($path)) {
  44. $this->setMessage($constraint->notFoundMessage, array('{{ file }}' => $path));
  45. return false;
  46. }
  47. if (!is_readable($path)) {
  48. $this->setMessage($constraint->notReadableMessage, array('{{ file }}' => $path));
  49. return false;
  50. }
  51. if ($constraint->maxSize) {
  52. if (ctype_digit((string) $constraint->maxSize)) {
  53. $size = filesize($path);
  54. $limit = $constraint->maxSize;
  55. $suffix = ' bytes';
  56. } else if (preg_match('/^(\d+)k$/', $constraint->maxSize, $matches)) {
  57. $size = round(filesize($path) / 1000, 2);
  58. $limit = $matches[1];
  59. $suffix = ' kB';
  60. } else if (preg_match('/^(\d+)M$/', $constraint->maxSize, $matches)) {
  61. $size = round(filesize($path) / 1000000, 2);
  62. $limit = $matches[1];
  63. $suffix = ' MB';
  64. } else {
  65. throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $constraint->maxSize));
  66. }
  67. if ($size > $limit) {
  68. $this->setMessage($constraint->maxSizeMessage, array(
  69. '{{ size }}' => $size.$suffix,
  70. '{{ limit }}' => $limit.$suffix,
  71. '{{ file }}' => $path,
  72. ));
  73. return false;
  74. }
  75. }
  76. if ($constraint->mimeTypes) {
  77. if (!$value instanceof FileObject) {
  78. $value = new FileObject($value);
  79. }
  80. if (!in_array($value->getMimeType(), (array) $constraint->mimeTypes)) {
  81. $this->setMessage($constraint->mimeTypesMessage, array(
  82. '{{ type }}' => '"'.$value->getMimeType().'"',
  83. '{{ types }}' => '"'.implode('", "', (array) $constraint->mimeTypes).'"',
  84. '{{ file }}' => $path,
  85. ));
  86. return false;
  87. }
  88. }
  89. return true;
  90. }
  91. }