File.php
Go to the documentation of this file.
1<?php
11
12define("UPLOAD_ERR_FILE_EXTENSION", 1000);
13
19class File extends Text
20{
21 protected $value = array();
22
26 protected $maxNum;
27
31 protected $maxSize;
32
37
47 protected function setDefaults()
48 {
49 parent::setDefaults();
50
51 // textClass elements have values of type string
52 $this->defaults['maxNum'] = 1;
53 $this->defaults['maxSize'] = false;
54 $this->defaults['allowedExtensions'] = "";
55 }
56
62 public function __toString()
63 {
64 if ($this->maxSize !== false) {
65 $maxInput = "<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"{$this->maxSize}\" />";
66 } else {
67 $maxInput = "";
68 }
69
70 $value = $this->htmlValue();
71 $inputAttributes = $this->htmlInputAttributes();
72 $marker = $this->htmlMarker();
73 $label = $this->htmlLabel();
74 $list = $this->htmlList();
75 $wrapperAttributes = $this->htmlWrapperAttributes();
78
79 return "<p {$wrapperAttributes}>" .
80 "<label>" .
81 "<span class=\"depage-label\">{$label}{$marker}</span>" .
82 $maxInput .
83 "<input name=\"{$this->name}[]\" type=\"{$this->type}\"{$inputAttributes}>" .
84 $list .
85 "</label>" .
88 "</p>\n";
89 }
95 protected function htmlInputAttributes()
96 {
97 $attributes = parent::htmlInputAttributes();
98
99 if ($this->maxNum > 1) {
100 $attributes .= " multiple=\"multiple\"";
101 }
102 if (!empty($this->allowedExtensions)) {
103 $attributes .= " accept=\"" . htmlentities($this->allowedExtensions) . "\"";
104 }
105
106 return $attributes;
107 }
108
114 protected function typeCastValue()
115 {
116 $this->value = (array) $this->value;
117 }
118
124 public function handleUploadedFiles($files = null)
125 {
126 if (!is_array($files)) {
127 $files = array();
128 }
129 $extRegex = "";
130 if (!empty($this->allowedExtensions)) {
131 $extRegex = str_replace(array(" ", ",", "."), array("", "|", "\."), $this->allowedExtensions);
132 }
133 if (isset($_FILES[$this->name])) {
134 foreach ($_FILES[$this->name]["error"] as $key => $error) {
135 if (!empty($extRegex) && !preg_match("/.*(" . $extRegex . ")$/i", $_FILES[$this->name]["name"][$key])) {
137 }
138 if ($error == UPLOAD_ERR_OK) {
139 $uploadName = $_FILES[$this->name]["tmp_name"][$key];
140 $tmpName = tempnam("", "depage-form-upload-");
141 $success = move_uploaded_file($uploadName, $tmpName);
142 if ($this->maxNum > 1) {
143 $files[] = array(
144 'name' => $_FILES[$this->name]["name"][$key],
145 'tmp_name' => $tmpName,
146 );
147
148 } else {
149 $files[0] = array(
150 'name' => $_FILES[$this->name]["name"][$key],
151 'tmp_name' => $tmpName,
152 );
153 }
154 } else {
155 $errorMsgs = array(
156 UPLOAD_ERR_INI_SIZE => "The uploaded file exceeds the upload_max_filesize directive in php.ini.",
157 UPLOAD_ERR_FORM_SIZE => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.",
158 UPLOAD_ERR_PARTIAL => "The uploaded file was only partially uploaded.",
159 UPLOAD_ERR_NO_FILE => "No file was uploaded.",
160 UPLOAD_ERR_NO_TMP_DIR => "Missing a temporary folder.",
161 UPLOAD_ERR_CANT_WRITE => "Failed to write file to disk.",
162 UPLOAD_ERR_EXTENSION => "A PHP extension stopped the file upload. PHP does not provide a way to ascertain which extension caused the file upload to stop.",
163 UPLOAD_ERR_FILE_EXTENSION => "The uploaded file has an unallowed extension.", // @todo add error message to form
164 );
165 $this->log("htmlform: " . $errorMsgs[$error]);
166 // TODO can't send array here
167 // $this->log($_FILES[$this->name]);
168 }
169 }
170 }
171
172 // truncate files at max
173 $this->value = array_slice($files, - $this->maxNum, $this->maxNum);
174
175 return $this->value;
176 }
177
183 public function clearValue()
184 {
185 $this->clearUploadedFiles();
186
187 $this->value = array();
188
189 return $this->value;
190 }
191
195 public function clearUploadedFiles()
196 {
197 if (count($this->value)) {
198 foreach ($this->value as $file) {
199 if (file_exists($file['tmp_name'])) {
200 unlink($file['tmp_name']);
201 }
202 }
203 }
204 }
205}
206
207/* vim:set ft=php sw=4 sts=4 fdm=marker et : */
log($argument, $type=null)
error & warning logger
Definition Element.php:242
$label
Input element - HTML label.
Definition Input.php:36
$errorMessage
Message that gets displayed in case of invalid input.
Definition Input.php:217
htmlHelpMessage()
Returns HTML-rendered helpMessage.
Definition Input.php:676
htmlWrapperAttributes()
Returns string of HTML attributes for element wrapper paragraph.
Definition Input.php:627
htmlMarker()
Returns elements' required-indicator.
Definition Input.php:565
$helpMessage
Extra help message.
Definition Input.php:229
htmlValue()
Returns HTML-rendered element value.
Definition Input.php:647
$marker
Input element - HTML marker text that marks required fields.
Definition Input.php:91
htmlErrorMessage()
Returns HTML-rendered error message.
Definition Input.php:657
HTML file input type.
Definition File.php:20
htmlInputAttributes()
renders text element specific HTML attributes
Definition File.php:95
$maxSize
HTML maxSize attribute.
Definition File.php:31
clearValue()
resets the value to en empty array and cleans uploaded files
Definition File.php:183
typeCastValue()
Converts value to element specific type.
Definition File.php:114
$maxNum
HTML maxNum attribute.
Definition File.php:26
handleUploadedFiles($files=null)
saves uploaded files
Definition File.php:124
__toString()
Renders element to HTML.
Definition File.php:62
clearUploadedFiles()
cleans uploaded files when session is cleared
Definition File.php:195
$allowedExtensions
HTML allowedExtensions attribute.
Definition File.php:36
setDefaults()
collects initial values across subclasses
Definition File.php:47
HTML text input type.
Definition Text.php:39
htmlList($options=null)
Renders HTML datalist.
Definition Text.php:129
Classes for HTML input-elements.
Definition Address.php:9
const UPLOAD_ERR_FILE_EXTENSION
Definition File.php:12