11namespace Depage\HtmlForm\Elements;
13use Depage\HtmlForm\Abstracts;
52 parent::setDefaults();
55 $this->defaults[
'maxNum'] = 1;
56 $this->defaults[
'maxSize'] =
false;
57 $this->defaults[
'allowedExtensions'] =
"";
67 if ($this->maxSize !==
false) {
68 $maxInput =
"<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"{$this->maxSize}\" />";
76 $label = $this->htmlLabel();
77 $list = $this->htmlList();
82 return "<p {$wrapperAttributes}>" .
84 "<span class=\"depage-label\">{$label}{$marker}</span>" .
86 "<input name=\"{$this->name}[]\" type=\"{$this->type}\"{$inputAttributes}>" .
100 $attributes = parent::htmlInputAttributes();
102 if ($this->maxNum > 1) {
103 $attributes .=
" multiple=\"multiple\"";
105 if (!empty($this->allowedExtensions)) {
106 $attributes .=
" accept=\"" . htmlentities($this->allowedExtensions) .
"\"";
119 $this->value = (array) $this->value;
129 if (!is_array($files)) {
133 if (!empty($this->allowedExtensions)) {
134 $extRegex = str_replace([
" ",
",",
"."], [
"",
"|",
"\."], $this->allowedExtensions);
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;
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) {
148 'tmp_name' => $tmpName,
154 'tmp_name' => $tmpName,
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.",
168 $this->
log(
"htmlform: " . $errorMsgs[$error]);
176 $this->value = array_slice($files, - $this->maxNum, $this->maxNum);
198 if (count($this->value)) {
199 foreach ($this->value as $file) {
200 if (file_exists($file[
'tmp_name'])) {
201 unlink($file[
'tmp_name']);
log(string $argument, string $type=null)
error & warning logger
$label
Input element - HTML label.
$errorMessage
Message that gets displayed in case of invalid input.
htmlHelpMessage()
Returns HTML-rendered helpMessage.
htmlWrapperAttributes()
Returns string of HTML attributes for element wrapper paragraph.
htmlMarker()
Returns elements' required-indicator.
$helpMessage
Extra help message.
htmlValue()
Returns HTML-rendered element value.
$marker
Input element - HTML marker text that marks required fields.
htmlErrorMessage()
Returns HTML-rendered error message.
htmlInputAttributes()
renders text element specific HTML attributes
handleUploadedFiles(array $files=null)
saves uploaded files
$maxSize
HTML maxSize attribute.
clearValue()
resets the value to en empty array and cleans uploaded files
typeCastValue()
Converts value to element specific type.
$maxNum
HTML maxNum attribute.
__toString()
Renders element to HTML.
clearUploadedFiles()
cleans uploaded files when session is cleared
$allowedExtensions
HTML allowedExtensions attribute.
setDefaults()
collects initial values across subclasses
const UPLOAD_ERR_FILE_EXTENSION