3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Validator\File
;
12 use Zend\Validator\AbstractValidator
;
13 use Zend\Validator\Exception
;
16 * Validator for the maximum size of a file up to a max of 2GB
19 class Upload
extends AbstractValidator
22 * @const string Error constants
24 const INI_SIZE
= 'fileUploadErrorIniSize';
25 const FORM_SIZE
= 'fileUploadErrorFormSize';
26 const PARTIAL
= 'fileUploadErrorPartial';
27 const NO_FILE
= 'fileUploadErrorNoFile';
28 const NO_TMP_DIR
= 'fileUploadErrorNoTmpDir';
29 const CANT_WRITE
= 'fileUploadErrorCantWrite';
30 const EXTENSION
= 'fileUploadErrorExtension';
31 const ATTACK
= 'fileUploadErrorAttack';
32 const FILE_NOT_FOUND
= 'fileUploadErrorFileNotFound';
33 const UNKNOWN
= 'fileUploadErrorUnknown';
36 * @var array Error message templates
38 protected $messageTemplates = array(
39 self
::INI_SIZE
=> "File '%value%' exceeds the defined ini size",
40 self
::FORM_SIZE
=> "File '%value%' exceeds the defined form size",
41 self
::PARTIAL
=> "File '%value%' was only partially uploaded",
42 self
::NO_FILE
=> "File '%value%' was not uploaded",
43 self
::NO_TMP_DIR
=> "No temporary directory was found for file '%value%'",
44 self
::CANT_WRITE
=> "File '%value%' can't be written",
45 self
::EXTENSION
=> "A PHP extension returned an error while uploading the file '%value%'",
46 self
::ATTACK
=> "File '%value%' was illegally uploaded. This could be a possible attack",
47 self
::FILE_NOT_FOUND
=> "File '%value%' was not found",
48 self
::UNKNOWN
=> "Unknown error while uploading file '%value%'"
51 protected $options = array(
56 * Sets validator options
58 * The array $files must be given in syntax of Zend\File\Transfer\Transfer to be checked
59 * If no files are given the $_FILES array will be used automatically.
60 * NOTE: This validator will only work with HTTP POST uploads!
62 * @param array|\Traversable $options Array of files in syntax of \Zend\File\Transfer\Transfer
64 public function __construct($options = array())
66 if (is_array($options) && !array_key_exists('files', $options)) {
67 $options = array('files' => $options);
70 parent
::__construct($options);
74 * Returns the array of set files
76 * @param string $file (Optional) The file to return in detail
78 * @throws Exception\InvalidArgumentException If file is not found
80 public function getFiles($file = null)
84 foreach ($this->options
['files'] as $name => $content) {
85 if ($name === $file) {
86 $return[$file] = $this->options
['files'][$name];
89 if ($content['name'] === $file) {
90 $return[$name] = $this->options
['files'][$name];
94 if (count($return) === 0) {
95 throw new Exception\
InvalidArgumentException("The file '$file' was not found");
101 return $this->options
['files'];
105 * Sets the files to be checked
107 * @param array $files The files to check in syntax of \Zend\File\Transfer\Transfer
108 * @return Upload Provides a fluent interface
110 public function setFiles($files = array())
112 if (count($files) === 0) {
113 $this->options
['files'] = $_FILES;
115 $this->options
['files'] = $files;
118 if ($this->options
['files'] === NULL) {
119 $this->options
['files'] = array();
122 foreach ($this->options
['files'] as $file => $content) {
123 if (!isset($content['error'])) {
124 unset($this->options
['files'][$file]);
132 * Returns true if and only if the file was uploaded without errors
134 * @param string $value Single file to check for upload errors, when giving null the $_FILES array
135 * from initialization will be used
139 public function isValid($value, $file = null)
142 $this->setValue($value);
143 if (array_key_exists($value, $this->getFiles())) {
144 $files = array_merge($files, $this->getFiles($value));
146 foreach ($this->getFiles() as $file => $content) {
147 if (isset($content['name']) && ($content['name'] === $value)) {
148 $files = array_merge($files, $this->getFiles($file));
151 if (isset($content['tmp_name']) && ($content['tmp_name'] === $value)) {
152 $files = array_merge($files, $this->getFiles($file));
158 return $this->throwError($file, self
::FILE_NOT_FOUND
);
161 foreach ($files as $file => $content) {
162 $this->value
= $file;
163 switch ($content['error']) {
165 if (!is_uploaded_file($content['tmp_name'])) {
166 $this->throwError($content, self
::ATTACK
);
171 $this->throwError($content, self
::INI_SIZE
);
175 $this->throwError($content, self
::FORM_SIZE
);
179 $this->throwError($content, self
::PARTIAL
);
183 $this->throwError($content, self
::NO_FILE
);
187 $this->throwError($content, self
::NO_TMP_DIR
);
191 $this->throwError($content, self
::CANT_WRITE
);
195 $this->throwError($content, self
::EXTENSION
);
199 $this->throwError($content, self
::UNKNOWN
);
204 if (count($this->getMessages()) > 0) {
212 * Throws an error of the given type
214 * @param string $file
215 * @param string $errorType
218 protected function throwError($file, $errorType)
220 if ($file !== null) {
221 if (is_array($file)) {
222 if (array_key_exists('name', $file)) {
223 $this->value
= $file['name'];
225 } elseif (is_string($file)) {
226 $this->value
= $file;
230 $this->error($errorType);