/[cvs]/nfo/php/libs/com.newsblob.phphtmllib/form/form_elements/FEFile.inc
ViewVC logotype

Contents of /nfo/php/libs/com.newsblob.phphtmllib/form/form_elements/FEFile.inc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (show annotations)
Thu May 6 16:27:26 2004 UTC (20 years, 4 months ago) by jonen
Branch: MAIN
Changes since 1.1: +243 -42 lines
 updated all to v2.4.1 - Apr 01, 2004

1 <?php
2 /**
3 * This file contains the FileUpload FormElement class.
4 *
5 * $Id: FEFile.inc,v 1.11 2004/03/25 21:02:59 hemna Exp $
6 *
7 * @author Walter A. Boring IV <waboring@buildabetterweb.com>
8 * @author Dave Brondsema <dave@brondsema.net>
9 * @package phpHtmlLib
10 * @subpackage FormProcessing
11 *
12 * @copyright LGPL - See LICENCE
13 *
14 */
15
16 /**
17 * This is the FileUpload FormElement which builds a
18 * input field of type="file". This automagically
19 * handles the case of a confirmation page.
20 *
21 * @author Dave Brondsema <dave@brondsema.net>
22 * @package phpHtmlLib
23 * @subpackage FormProcessing
24 *
25 * @copyright LGPL - See LICENCE
26 */
27 class FEFile extends FEText {
28
29
30 /**
31 * The $_FILES information about
32 * the submitted file.
33 */
34 var $_files_info = array('name' => '',
35 'type' => '',
36 'size' => '',
37 'tmp_name' => '');
38
39 /**
40 * The place where we temporarily save
41 * the file during confirmation
42 */
43 var $_temp_dir = "/tmp";
44
45 /**
46 * Array listing mime types to check for
47 * during validation
48 */
49 var $_valid_types = array();
50
51 /**
52 * max size in bytes determined by user
53 *
54 */
55 var $_max_size;
56
57 /**
58 * This function builds and returns the
59 * form element object
60 *
61 * @return object
62 */
63 function get_element() {
64 $attributes = $this->_build_element_attributes();
65 $attributes["type"] = "file";
66
67 if (($value = $this->get_value()) != NULL)
68 $attributes["value"] = $value;
69
70 $tag = new INPUTtag($attributes);
71
72 return $tag;
73 }
74
75 /**
76 * This method validates the data
77 * for this Form Element.
78 *
79 * It validates file size and partial uploads..
80 * @param FormValidation object.
81 */
82 function validate(&$_FormValidation) {
83 switch ($_FILES[$this->get_element_name()]['error']) {
84 case 0:
85 if ($_FILES[$this->get_element_name()]['size'] == 0) {
86 $this->set_error_message("The uploaded file was empty.");
87 return FALSE;
88 }
89 break;
90 case 1:
91 $this->set_error_message("The uploaded file exceeds the maximum file size, " .
92 ini_get("upload_max_filesize"));
93 return FALSE;
94 break;
95 case 2:
96 $this->set_error_message("The uploaded file exceeds the maximum file size, " .
97 $_REQUEST['MAX_FILE_SIZE'] . " bytes, specified for this form.");
98 return FALSE;
99 break;
100 case 3:
101 $this->set_error_message("The uploaded file was only partially uploaded.");
102 return FALSE;
103 break;
104 case 4:
105 //make sure that we are required to have a result
106 if ($this->is_required()) {
107 $this->set_error_message("No file was uploaded.");
108 return FALSE;
109 } else {
110 //we aren't required,
111 //and we don't have a value.
112 //this is a special case of
113 //parent::do_validation
114 return TRUE;
115 }
116 break;
117
118 case 5:
119 $this->set_error_message("The uploaded file was empty.");
120 return FALSE;
121 break;
122 }
123
124 if (!empty($this->_valid_types)) {
125 if (!in_array($_FILES[$this->get_element_name()]['type'], $this->_valid_types)) {
126 $this->set_error_message('Not a valid file type.');
127 return FALSE;
128 }
129 }
130
131 if (!empty($this->_max_size)) {
132 if ($_FILES[$this->get_element_name()]['size'] > $this->_max_size) {
133 $this->set_error_message('File can be no larger than ' . $this->_max_size . ' bytes');
134 return FALSE;
135 }
136 }
137
138 return TRUE;
139 }
140
141 /**
142 * This function will return the
143 * elements value
144 *
145 * @return mixed
146 */
147 function get_value() {
148 //do we need to repopulate
149 //the file_info?
150 if (isset($_REQUEST[FORM_CONFIRM]) && empty($this->_files_info['name'])) {
151 $this->_populate_file_info();
152
153 //let the FormContent do what they want.
154 return $this->_files_info;
155 } else {
156 if (isset($_FILES[$this->get_element_name()]['name'])) {
157 return $_FILES[$this->get_element_name()]['name'];
158 } else {
159 return '';
160 }
161 }
162 }
163
164 /**
165 *
166 * @param mixed - the value to look up
167 * @return string - the text associated
168 */
169 function get_value_text() {
170 if (empty($this->_files_info['name'])) {
171 return '';
172 } else {
173 return $this->_files_info['name']." (".
174 $this->_files_info['type'].") ".
175 $this->_files_info['size']." bytes";
176 }
177 }
178
179 /**
180 * This is so we can save the file information
181 * in a hidden form field during confirmation
182 * page.
183 *
184 * @return string
185 */
186 function get_pre_confirm_value() {
187 return urlencode(serialize($this->_files_info));
188 }
189
190
191
192 /**
193 * This function will return this file's portion of the $_FILES array
194 *
195 * @return array
196 */
197 function get_file_info() {
198 return $_FILES[$this->get_element_name()];
199 }
200
201 /**
202 * This is so the user can set the temp directory
203 * where the file will be saved during confirmation
204 * (if any)
205 *
206 * @param string the new temp dir.
207 */
208 function set_temp_dir($dir) {
209 $this->_temp_dir = $dir;
210 }
211
212 /**
213 * This is so the user can get the temp directory
214 * where the file was saved during confirmation
215 * (if any)
216 *
217 * @return string the new temp dir.
218 */
219 function get_temp_dir() {
220 return $this->_temp_dir;
221 }
222
223
224 /**
225 * The function that allows us to save the
226 * temp file someplace we can use it after
227 * a confirmation has been accepted.
228 *
229 * NOTE: if the user doesn't confirm, then
230 * this could leave files in /tmp
231 *
232 */
233 function _pre_confirm() {
234 $name = $this->get_element_name();
235 if (!empty($_FILES[$name]['name'])) {
236 //ok we need to move the file so that the web
237 //server doesn't nuke it once the request is done.
238 $this->_files_info['name'] = $_FILES[$name]["name"];
239 $this->_files_info['type'] = $_FILES[$name]['type'];
240 $this->_files_info['size'] = $_FILES[$name]['size'];
241
242 $tempname = tempnam($this->_temp_dir, get_class($this));
243 $this->_files_info['tmp_name'] = str_replace($this->_temp_dir, '', $tempname);
244 rename($_FILES[$name]['tmp_name'], $tempname);
245 }
246
247 return TRUE;
248 }
249
250 /**
251 * This method re-populates the $this->_files_info
252 * because the form had a confirmation page,
253 * and the user accepted the confirmation.
254 *
255 * This is so we can re populate the local private var
256 * so we can get access at the file data on disk.
257 *
258 */
259 function _populate_file_info() {
260 $this->_files_info = unserialize(urldecode($this->_value));
261 $this->_value = $this->_files_info['name'];
262 $this->_files_info['tmp_name'] = $this->_temp_dir.$this->_files_info['tmp_name'];
263 }
264
265
266 /**
267 * This method returns the hidden version of this
268 * element for a confirmation page.
269 *
270 * NOTE: This is called by the FormProcessor only.
271 * It shouldn't be called manually.
272 *
273 * @return INPUTtag of type hidden
274 */
275 function get_confirm_element() {
276 return form_hidden($this->get_element_name(),
277 $this->get_pre_confirm_value() );
278 }
279
280 /**
281 * This is so the user can create a list of mime types
282 * to allow
283 *
284 * @param string a mime type to check for
285 */
286 function add_valid_type($mime_type) {
287 $this->_valid_types[] = $mime_type;
288 }
289
290 /**
291 * This allows the user to set the max size of the file
292 *
293 * @param integer max size in bytes
294 */
295 function set_max_size($max_size) {
296 $this->_max_size = $max_size;
297 }
298
299 /**
300 * This allows the user to get the max size of the file
301 * (if any has been set)
302 *
303 * @return integer value of $_max_size if any
304 */
305 function get_max_size($max_size) {
306 return $this->_max_size;
307 }
308 }
309 ?>

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed