7
* This source file is subject to the new BSD license that is bundled
8
* with this package in the file LICENSE.txt.
9
* It is also available through the world-wide-web at this URL:
10
* http://framework.zend.com/license/new-bsd
11
* If you did not receive a copy of the license and are unable to
12
* obtain it through the world-wide-web, please send an email
13
* to license@zend.com so we can send you a copy immediately.
16
* @package Zend_Validate
17
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
18
* @license http://framework.zend.com/license/new-bsd New BSD License
23
* @see Zend_Validate_Abstract
25
require_once 'Zend/Validate/Abstract.php';
28
* Validator for the maximum size of a file up to a max of 2GB
31
* @package Zend_Validate
32
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
33
* @license http://framework.zend.com/license/new-bsd New BSD License
35
class Zend_Validate_File_Upload extends Zend_Validate_Abstract
38
* @const string Error constants
40
const INI_SIZE = 'fileUploadErrorIniSize';
41
const FORM_SIZE = 'fileUploadErrorFormSize';
42
const PARTIAL = 'fileUploadErrorPartial';
43
const NO_FILE = 'fileUploadErrorNoFile';
44
const NO_TMP_DIR = 'fileUploadErrorNoTmpDir';
45
const CANT_WRITE = 'fileUploadErrorCantWrite';
46
const EXTENSION = 'fileUploadErrorExtension';
47
const ATTACK = 'fileUploadErrorAttack';
48
const FILE_NOT_FOUND = 'fileUploadErrorFileNotFound';
49
const UNKNOWN = 'fileUploadErrorUnknown';
53
* @var array Error message templates
55
protected $_messageTemplates = array(
56
self::INI_SIZE => "The file '%value%' exceeds the defined ini size",
57
self::FORM_SIZE => "The file '%value%' exceeds the defined form size",
58
self::PARTIAL => "The file '%value%' was only partially uploaded",
59
self::NO_FILE => "The file '%value%' was not uploaded",
60
self::NO_TMP_DIR => "No temporary directory was found for the file '%value%'",
61
self::CANT_WRITE => "The file '%value%' can't be written",
62
self::EXTENSION => "The extension returned an error while uploading the file '%value%'",
63
self::ATTACK => "The file '%value%' was illegal uploaded, possible attack",
64
self::FILE_NOT_FOUND => "The file '%value%' was not found",
65
self::UNKNOWN => "Unknown error while uploading the file '%value%'"
69
* Internal array of files
72
protected $_files = array();
75
* Sets validator options
77
* The array $files must be given in syntax of Zend_File_Transfer to be checked
78
* If no files are given the $_FILES array will be used automatically.
79
* NOTE: This validator will only work with HTTP POST uploads!
81
* @param array $files Array of files in syntax of Zend_File_Transfer
84
public function __construct($files = array())
86
$this->setFiles($files);
90
* Returns the array of set files
92
* @param string $files (Optional) The file to return in detail
94
* @throws Zend_Validate_Exception If file is not found
96
public function getFiles($file = null)
100
foreach ($this->_files as $name => $content) {
101
if ($name === $file) {
102
$return[$file] = $this->_files[$name];
105
if ($content['name'] === $file) {
106
$return[$name] = $this->_files[$name];
110
if (count($return) === 0) {
111
require_once 'Zend/Validate/Exception.php';
112
throw new Zend_Validate_Exception("The file '$file' was not found");
118
return $this->_files;
122
* Sets the minimum filesize
124
* @param array $files The files to check in syntax of Zend_File_Transfer
125
* @return Zend_Validate_File_Upload Provides a fluent interface
127
public function setFiles($files = array())
129
if (count($files) === 0) {
130
$this->_files = $_FILES;
132
$this->_files = $files;
138
* Defined by Zend_Validate_Interface
140
* Returns true if and only if the file was uploaded without errors
142
* @param string $value Single file to check for upload errors, when giving null the $_FILES array
143
* from initialization will be used
146
public function isValid($value, $file = null)
148
if (array_key_exists($value, $this->_files)) {
149
$files[$value] = $this->_files[$value];
151
foreach ($this->_files as $file => $content) {
152
if ($content['name'] === $value) {
153
$files[$file] = $this->_files[$file];
156
if ($content['tmp_name'] === $value) {
157
$files[$file] = $this->_files[$file];
163
return $this->_throw($file, self::FILE_NOT_FOUND);
166
foreach ($files as $file => $content) {
167
$this->_value = $file;
168
switch($content['error']) {
170
if (!is_uploaded_file($content['tmp_name'])) {
171
$this->_throw($file, self::ATTACK);
176
$this->_throw($file, self::INI_SIZE);
180
$this->_throw($file, self::FORM_SIZE);
184
$this->_throw($file, self::PARTIAL);
188
$this->_throw($file, self::NO_FILE);
192
$this->_throw($file, self::NO_TMP_DIR);
196
$this->_throw($file, self::CANT_WRITE);
200
$this->_throw($file, self::EXTENSION);
204
$this->_throw($file, self::UNKNOWN);
209
if (count($this->_messages) > 0) {
217
* Throws an error of the given type
219
* @param string $file
220
* @param string $errorType
223
protected function _throw($file, $errorType)
225
if ($file !== null) {
226
if (is_array($file) and !empty($file['name'])) {
227
$this->_value = $file['name'];
231
$this->_error($errorType);