upload_of.php 838 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php namespace System\Validation\Rules;
  2. use System\Input;
  3. use System\Validation\Rule;
  4. class Upload_Of extends Rule {
  5. /**
  6. * The acceptable file extensions.
  7. *
  8. * @var array
  9. */
  10. public $extensions;
  11. /**
  12. * The maximum file size in bytes.
  13. *
  14. * @var int
  15. */
  16. public $maximum;
  17. /**
  18. * Evaluate the validity of an attribute.
  19. *
  20. * @param string $attribute
  21. * @param array $attributes
  22. * @return void
  23. */
  24. public function check($attribute, $attributes)
  25. {
  26. if ( ! array_key_exists($attribute, Input::file()))
  27. {
  28. return true;
  29. }
  30. $file = Input::file($attribute);
  31. if ( ! is_null($this->maximum) and $file['size'] > $this->maximum)
  32. {
  33. return false;
  34. }
  35. if ( ! is_null($this->extensions) and ! in_array(File::extension($file['name']), $this->extensions))
  36. {
  37. return false;
  38. }
  39. return true;
  40. }
  41. }