file.php
Go to the documentation of this file.
1 <?php
10 namespace depage\htmlform\elements;
11 
17 class file extends text
18 {
19  protected $value = array();
20 
30  protected function setDefaults()
31  {
32  parent::setDefaults();
33 
34  // textClass elements have values of type string
35  $this->defaults['maxNum'] = 1;
36  $this->defaults['maxSize'] = false;
37  }
38 
44  public function __toString()
45  {
46  if ($this->maxSize !== false) {
47  $maxInput = "<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"{$this->maxSize}\" />";
48  } else {
49  $maxInput = "";
50  }
51 
52  $value = $this->htmlValue();
53  $inputAttributes = $this->htmlInputAttributes();
54  $marker = $this->htmlMarker();
55  $label = $this->htmlLabel();
56  $list = $this->htmlList();
57  $wrapperAttributes = $this->htmlWrapperAttributes();
59  $helpMessage = $this->htmlHelpMessage();
60 
61  return "<p {$wrapperAttributes}>" .
62  "<label>" .
63  "<span class=\"depage-label\">{$label}{$marker}</span>" .
64  $maxInput .
65  "<input name=\"{$this->name}[]\" type=\"{$this->type}\"{$inputAttributes}>" .
66  $list .
67  "</label>" .
69  $helpMessage .
70  "</p>\n";
71  }
77  protected function htmlInputAttributes()
78  {
79  $attributes = parent::htmlInputAttributes();
80 
81  if ($this->maxNum > 1) {
82  $attributes .= " multiple=\"multiple\"";
83  }
84 
85  return $attributes;
86  }
87 
93  protected function typeCastValue()
94  {
95  $this->value = (array) $this->value;
96  }
97 
103  public function handleUploadedFiles($files = null)
104  {
105  if (!is_array($files)) {
106  $files = array();
107  }
108  if (isset($_FILES[$this->name])) {
109  foreach ($_FILES[$this->name]["error"] as $key => $error) {
110  if ($error == UPLOAD_ERR_OK) {
111  $upload_name = $_FILES[$this->name]["tmp_name"][$key];
112  $tmp_name = tempnam(null, "depage-form-upload-");
113  if (move_uploaded_file($upload_name, $tmp_name)) {
114  if ($this->maxNum > 1) {
115  $files[] = array(
116  'name' => $_FILES[$this->name]["name"][$key],
117  'tmp_name' => $tmp_name,
118  );
119 
120  } else {
121  $files[0] = array(
122  'name' => $_FILES[$this->name]["name"][$key],
123  'tmp_name' => $tmp_name,
124  );
125  }
126  } else {
127  $errorMsgs = array(
128  UPLOAD_ERR_INI_SIZE => "The uploaded file exceeds the upload_max_filesize directive in php.ini.",
129  UPLOAD_ERR_FORM_SIZE => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.",
130  UPLOAD_ERR_PARTIAL => "The uploaded file was only partially uploaded.",
131  UPLOAD_ERR_NO_FILE => "No file was uploaded.",
132  UPLOAD_ERR_NO_TMP_DIR => "Missing a temporary folder.",
133  UPLOAD_ERR_CANT_WRITE => "Failed to write file to disk.",
134  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.",
135  );
136  $this->log("htmlform: " . $errorMsgs[$error]);
137  // TODO can't send array here
138  // $this->log($_FILES[$this->name]);
139  }
140  }
141  }
142  }
143 
144  // truncate files at max
145  $this->value = array_slice($files, -$this->maxNum, $this->maxNum);
146 
147  return $this->value;
148  }
149 
155  public function clearValue()
156  {
157  $this->clearUploadedFiles();
158 
159  $this->value = array();
160  }
161 
165  public function clearUploadedFiles()
166  {
167  if (count($this->value)) {
168  foreach ($this->value as $file) {
169  unlink($file['tmp_name']);
170  }
171  }
172  }
173 }
174 
175 /* vim:set ft=php sw=4 sts=4 fdm=marker et : */
clearValue()
resets the value to en empty array and cleans uploaded files
Definition: file.php:155
HTML file input type.
Definition: file.php:17
htmlWrapperAttributes()
Returns string of HTML attributes for element wrapper paragraph.
Definition: input.php:486
htmlMarker()
Returns elements&#39; required-indicator.
Definition: input.php:445
htmlErrorMessage()
Returns HTML-rendered error message.
Definition: input.php:514
HTML text input type.
Definition: text.php:38
htmlValue()
Returns HTML-rendered element value.
Definition: input.php:504
htmlHelpMessage()
Returns HTML-rendered helpMessage.
Definition: input.php:533
$label
Input element - HTML label.
Definition: input.php:36
handleUploadedFiles($files=null)
saves uploaded files
Definition: file.php:103
clearUploadedFiles()
cleans uploaded files when session is cleared
Definition: file.php:165
log($argument, $type=null)
error &amp; warning logger
Definition: element.php:168
$errorMessage
Message that gets displayed in case of invalid input.
Definition: input.php:173
setDefaults()
collects initial values across subclasses
Definition: file.php:30
__toString()
Renders element to HTML.
Definition: file.php:44
htmlList($options=null)
Renders HTML datalist.
Definition: text.php:112
$helpMessage
Extra help message.
Definition: input.php:185
$marker
Input element - HTML marker text that marks required fields.
Definition: input.php:47
typeCastValue()
Converts value to element specific type.
Definition: file.php:93
htmlInputAttributes()
renders text element specific HTML attributes
Definition: file.php:77