depage-forms v1.4.1
html forms made easy
Loading...
Searching...
No Matches
File.php
Go to the documentation of this file.
1<?php
2
10
11namespace Depage\HtmlForm\Elements;
12
13use Depage\HtmlForm\Abstracts;
14
20class File extends Abstracts\Input
21{
23
24 protected $value = [];
25
29 protected $maxNum;
30
34 protected $maxSize;
35
40
50 protected function setDefaults(): void
51 {
52 parent::setDefaults();
53
54 // textClass elements have values of type string
55 $this->defaults['maxNum'] = 1;
56 $this->defaults['maxSize'] = false;
57 $this->defaults['allowedExtensions'] = "";
58 }
59
65 public function __toString(): string
66 {
67 if ($this->maxSize !== false) {
68 $maxInput = "<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"{$this->maxSize}\" />";
69 } else {
70 $maxInput = "";
71 }
72
73 $value = $this->htmlValue();
74 $inputAttributes = $this->htmlInputAttributes();
75 $marker = $this->htmlMarker();
76 $label = $this->htmlLabel();
77 $list = $this->htmlList();
78 $wrapperAttributes = $this->htmlWrapperAttributes();
81
82 return "<p {$wrapperAttributes}>" .
83 "<label>" .
84 "<span class=\"depage-label\">{$label}{$marker}</span>" .
85 $maxInput .
86 "<input name=\"{$this->name}[]\" type=\"{$this->type}\"{$inputAttributes}>" .
87 $list .
88 "</label>" .
91 "</p>\n";
92 }
93
98 protected function htmlInputAttributes(): string
99 {
100 $attributes = parent::htmlInputAttributes();
101
102 if ($this->maxNum > 1) {
103 $attributes .= " multiple=\"multiple\"";
104 }
105 if (!empty($this->allowedExtensions)) {
106 $attributes .= " accept=\"" . htmlentities($this->allowedExtensions) . "\"";
107 }
108
109 return $attributes;
110 }
111
117 protected function typeCastValue(): void
118 {
119 $this->value = (array) $this->value;
120 }
121
127 public function handleUploadedFiles(array $files = null): array
128 {
129 if (!is_array($files)) {
130 $files = [];
131 }
132 $extRegex = "";
133 if (!empty($this->allowedExtensions)) {
134 $extRegex = str_replace([" ", ",", "."], ["", "|", "\."], $this->allowedExtensions);
135 }
136 if (isset($_FILES[$this->name])) {
137 foreach ($_FILES[$this->name]["error"] as $key => $error) {
138 if (!empty($extRegex) && !preg_match("/.*(" . $extRegex . ")$/i", $_FILES[$this->name]["name"][$key])) {
139 $error = self::UPLOAD_ERR_FILE_EXTENSION;
140 }
141 if ($error == UPLOAD_ERR_OK) {
142 $uploadName = $_FILES[$this->name]["tmp_name"][$key];
143 $tmpName = tempnam("", "depage-form-upload-");
144 $success = move_uploaded_file($uploadName, $tmpName);
145 if ($this->maxNum > 1) {
146 $files[] = [
147 'name' => $_FILES[$this->name]["name"][$key],
148 'tmp_name' => $tmpName,
149 ];
150
151 } else {
152 $files[0] = [
153 'name' => $_FILES[$this->name]["name"][$key],
154 'tmp_name' => $tmpName,
155 ];
156 }
157 } else {
158 $errorMsgs = [
159 UPLOAD_ERR_INI_SIZE => "The uploaded file exceeds the upload_max_filesize directive in php.ini.",
160 UPLOAD_ERR_FORM_SIZE => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.",
161 UPLOAD_ERR_PARTIAL => "The uploaded file was only partially uploaded.",
162 UPLOAD_ERR_NO_FILE => "No file was uploaded.",
163 UPLOAD_ERR_NO_TMP_DIR => "Missing a temporary folder.",
164 UPLOAD_ERR_CANT_WRITE => "Failed to write file to disk.",
165 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.",
166 self::UPLOAD_ERR_FILE_EXTENSION => "The uploaded file has an unallowed extension.", // @todo add error message to form
167 ];
168 $this->log("htmlform: " . $errorMsgs[$error]);
169 // TODO can't send array here
170 // $this->log($_FILES[$this->name]);
171 }
172 }
173 }
174
175 // truncate files at max
176 $this->value = array_slice($files, - $this->maxNum, $this->maxNum);
177
178 return $this->value;
179 }
180
186 public function clearValue(): void
187 {
188 $this->clearUploadedFiles();
189
190 $this->value = [];
191 }
192
196 public function clearUploadedFiles(): void
197 {
198 if (count($this->value)) {
199 foreach ($this->value as $file) {
200 if (file_exists($file['tmp_name'])) {
201 unlink($file['tmp_name']);
202 }
203 }
204 }
205 }
206}
207
208/* vim:set ft=php sw=4 sts=4 fdm=marker et : */
log(string $argument, string $type=null)
error & warning logger
Definition Element.php:236
input element base class
Definition Input.php:22
$label
Input element - HTML label.
Definition Input.php:37
$errorMessage
Message that gets displayed in case of invalid input.
Definition Input.php:218
htmlHelpMessage()
Returns HTML-rendered helpMessage.
Definition Input.php:685
htmlWrapperAttributes()
Returns string of HTML attributes for element wrapper paragraph.
Definition Input.php:636
htmlMarker()
Returns elements' required-indicator.
Definition Input.php:566
$helpMessage
Extra help message.
Definition Input.php:230
htmlValue()
Returns HTML-rendered element value.
Definition Input.php:656
$marker
Input element - HTML marker text that marks required fields.
Definition Input.php:92
htmlErrorMessage()
Returns HTML-rendered error message.
Definition Input.php:666
HTML file input type.
Definition File.php:21
htmlInputAttributes()
renders text element specific HTML attributes
Definition File.php:98
handleUploadedFiles(array $files=null)
saves uploaded files
Definition File.php:127
$maxSize
HTML maxSize attribute.
Definition File.php:34
clearValue()
resets the value to en empty array and cleans uploaded files
Definition File.php:186
typeCastValue()
Converts value to element specific type.
Definition File.php:117
$maxNum
HTML maxNum attribute.
Definition File.php:29
__toString()
Renders element to HTML.
Definition File.php:65
clearUploadedFiles()
cleans uploaded files when session is cleared
Definition File.php:196
$allowedExtensions
HTML allowedExtensions attribute.
Definition File.php:39
setDefaults()
collects initial values across subclasses
Definition File.php:50