upload_of.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php namespace System\Validation\Rules;
  2. use System\File;
  3. use System\Input;
  4. use System\Validation\Nullable_Rule;
  5. class Upload_Of extends Nullable_Rule {
  6. /**
  7. * The acceptable file types.
  8. *
  9. * @var array
  10. */
  11. public $types = array();
  12. /**
  13. * The maximum file size in bytes.
  14. *
  15. * @var int
  16. */
  17. public $maximum;
  18. /**
  19. * The "wrong type" error message.
  20. *
  21. * @var string
  22. */
  23. public $wrong_type;
  24. /**
  25. * The "too big" error message.
  26. *
  27. * @var string
  28. */
  29. public $too_big;
  30. /**
  31. * Evaluate the validity of an attribute.
  32. *
  33. * @param string $attribute
  34. * @param array $attributes
  35. * @return bool
  36. */
  37. public function check($attribute, $attributes)
  38. {
  39. // -----------------------------------------------------
  40. // Check the presence of the upload. If the upload does
  41. // not exist and the upload is required, a presence_of
  42. // error will be raised.
  43. //
  44. // Otherwise no error will be raised.
  45. // -----------------------------------------------------
  46. if ( ! array_key_exists($attribute, Input::file()))
  47. {
  48. if ( ! $this->allow_null)
  49. {
  50. $this->error = 'presence_of';
  51. }
  52. return is_null($this->error);
  53. }
  54. // -----------------------------------------------------
  55. // Uploaded files are stored in the $_FILES array, so
  56. // we use that array instead of the $attributes.
  57. // -----------------------------------------------------
  58. $file = Input::file($attribute);
  59. if ( ! is_null($this->maximum) and $file['size'] > $this->maximum * 1000)
  60. {
  61. $this->error = 'file_too_big';
  62. }
  63. // -----------------------------------------------------
  64. // The File::is method uses the Fileinfo PHP extension
  65. // to determine the MIME type of the file.
  66. // -----------------------------------------------------
  67. foreach ($this->types as $type)
  68. {
  69. if (File::is($type, $file['tmp_name']))
  70. {
  71. break;
  72. }
  73. $this->error = 'file_wrong_type';
  74. }
  75. return is_null($this->error);
  76. }
  77. /**
  78. * Set the acceptable file types.
  79. *
  80. * @return Upload_Of
  81. */
  82. public function is()
  83. {
  84. $this->types = func_get_args();
  85. return $this;
  86. }
  87. /**
  88. * Require that the uploaded file is an image type.
  89. *
  90. * @return Upload_Of
  91. */
  92. public function is_image()
  93. {
  94. $this->types = array_merge($this->types, array('jpg', 'gif', 'png', 'bmp'));
  95. return $this;
  96. }
  97. /**
  98. * Set the maximum file size in kilobytes.
  99. *
  100. * @param int $maximum
  101. * @return Upload_Of
  102. */
  103. public function maximum($maximum)
  104. {
  105. $this->maximum = $maximum;
  106. return $this;
  107. }
  108. /**
  109. * Set the validation error message.
  110. *
  111. * @param string $message
  112. * @return Upload_Of
  113. */
  114. public function message($message)
  115. {
  116. return $this->wrong_type($message)->too_big($message);
  117. }
  118. /**
  119. * Set the "wrong type" error message.
  120. *
  121. * @param string $message
  122. * @return Upload_Of
  123. */
  124. public function wrong_type($message)
  125. {
  126. $this->wrong_type = $message;
  127. return $this;
  128. }
  129. /**
  130. * Set the "too big" error message.
  131. *
  132. * @param string $message
  133. * @return Upload_Of
  134. */
  135. public function too_big($message)
  136. {
  137. $this->too_big = $message;
  138. return $this;
  139. }
  140. }