| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* TFileUpload class file
|
|
|
4 |
*
|
|
|
5 |
* @author Marcus Nyeholt <tanus@users.sourceforge.net>, Qiang Xue <qiang.xue@gmail.com>
|
|
|
6 |
* @link http://www.pradosoft.com/
|
|
|
7 |
* @copyright Copyright © 2005-2008 PradoSoft
|
|
|
8 |
* @license http://www.pradosoft.com/license/
|
|
|
9 |
* @version $Id: TFileUpload.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
10 |
* @package System.Web.UI.WebControls
|
|
|
11 |
*/
|
|
|
12 |
|
|
|
13 |
/**
|
|
|
14 |
* TFileUpload class
|
|
|
15 |
*
|
|
|
16 |
* TFileUpload displays a file upload field on a page. Upon postback,
|
|
|
17 |
* the text entered into the field will be treated as the name of the file
|
|
|
18 |
* that will be uploaded to the server. The property {@link getHasFile HasFile}
|
|
|
19 |
* indicates whether the file upload is successful. If successful, the file
|
|
|
20 |
* may be obtained by calling {@link saveAs} to save it at a specified place.
|
|
|
21 |
* You can use {@link getFileName FileName}, {@link getFileType FileType},
|
|
|
22 |
* {@link getFileSize FileSize} to get the original client-side file name,
|
|
|
23 |
* the file mime type, and the file size information. If the upload is not
|
|
|
24 |
* successful, {@link getErrorCode ErrorCode} contains the error code
|
|
|
25 |
* describing the cause of failure.
|
|
|
26 |
*
|
|
|
27 |
* TFileUpload raises {@link onFileUpload OnFileUpload} event if a file is uploaded
|
|
|
28 |
* (whether it succeeds or not).
|
|
|
29 |
*
|
|
|
30 |
* @author Marcus Nyeholt <tanus@users.sourceforge.net>, Qiang Xue <qiang.xue@gmail.com>
|
|
|
31 |
* @version $Id: TFileUpload.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
32 |
* @package System.Web.UI.WebControls
|
|
|
33 |
* @since 3.0
|
|
|
34 |
*/
|
|
|
35 |
class TFileUpload extends TWebControl implements IPostBackDataHandler, IValidatable
|
|
|
36 |
{
|
|
|
37 |
/**
|
|
|
38 |
* Maximum file size (in bytes) allowed to be uploaded, defaults to 1MB.
|
|
|
39 |
*/
|
|
|
40 |
const MAX_FILE_SIZE=1048576;
|
|
|
41 |
/**
|
|
|
42 |
* @var integer the size of the uploaded file (in bytes)
|
|
|
43 |
*/
|
|
|
44 |
private $_fileSize=0;
|
|
|
45 |
/**
|
|
|
46 |
* @var string The original name of the file on the client machine
|
|
|
47 |
*/
|
|
|
48 |
private $_fileName='';
|
|
|
49 |
/**
|
|
|
50 |
* @var string the name of the temporary file storing the uploaded file
|
|
|
51 |
*/
|
|
|
52 |
private $_localName='';
|
|
|
53 |
/**
|
|
|
54 |
* @var string the uploaded file mime type
|
|
|
55 |
*/
|
|
|
56 |
private $_fileType='';
|
|
|
57 |
/**
|
|
|
58 |
* @var integer error code of the current file upload
|
|
|
59 |
*/
|
|
|
60 |
private $_errorCode=UPLOAD_ERR_NO_FILE;
|
|
|
61 |
private $_dataChanged=false;
|
|
|
62 |
private $_isValid=true;
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* @return string tag name of the file upload control
|
|
|
66 |
*/
|
|
|
67 |
protected function getTagName()
|
|
|
68 |
{
|
|
|
69 |
return 'input';
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* Sets name attribute to the unique ID of the control.
|
|
|
74 |
* This method overrides the parent implementation with additional file update control specific attributes.
|
|
|
75 |
* @param THtmlWriter the writer used for the rendering purpose
|
|
|
76 |
*/
|
|
|
77 |
protected function addAttributesToRender($writer)
|
|
|
78 |
{
|
|
|
79 |
$this->getPage()->ensureRenderInForm($this);
|
|
|
80 |
parent::addAttributesToRender($writer);
|
|
|
81 |
$writer->addAttribute('type','file');
|
|
|
82 |
$writer->addAttribute('name',$this->getUniqueID());
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
/**
|
|
|
86 |
* Sets Enctype of the form on the page.
|
|
|
87 |
* This method overrides the parent implementation and is invoked before render.
|
|
|
88 |
* @param mixed event parameter
|
|
|
89 |
*/
|
|
|
90 |
public function onPreRender($param)
|
|
|
91 |
{
|
|
|
92 |
parent::onPreRender($param);
|
|
|
93 |
if(($form=$this->getPage()->getForm())!==null)
|
|
|
94 |
$form->setEnctype('multipart/form-data');
|
|
|
95 |
$this->getPage()->getClientScript()->registerHiddenField('MAX_FILE_SIZE',$this->getMaxFileSize());
|
|
|
96 |
if($this->getEnabled(true))
|
|
|
97 |
$this->getPage()->registerRequiresPostData($this);
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
/**
|
|
|
101 |
* @return integer the maximum file size, defaults to 1MB (1048576 bytes).
|
|
|
102 |
* @see setMaxFileSize
|
|
|
103 |
*/
|
|
|
104 |
public function getMaxFileSize()
|
|
|
105 |
{
|
|
|
106 |
return $this->getViewState('MaxFileSize',self::MAX_FILE_SIZE);
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
/**
|
|
|
110 |
* Sets the maximum size that a file can be uploaded.
|
|
|
111 |
* Note, this is an advisory value to the browser. Sets this property with
|
|
|
112 |
* a reasonably large size to save users the trouble of waiting
|
|
|
113 |
* for a big file being transferred only to find that it was too big
|
|
|
114 |
* and the transfer failed.
|
|
|
115 |
* @param int the maximum upload size allowed for a file.
|
|
|
116 |
*/
|
|
|
117 |
public function setMaxFileSize($size)
|
|
|
118 |
{
|
|
|
119 |
$this->setViewState('MaxFileSize',TPropertyValue::ensureInteger($size),self::MAX_FILE_SIZE);
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
/**
|
|
|
123 |
* @return string the original full path name of the file on the client machine
|
|
|
124 |
*/
|
|
|
125 |
public function getFileName()
|
|
|
126 |
{
|
|
|
127 |
return $this->_fileName;
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
/**
|
|
|
131 |
* @return integer the actual size of the uploaded file in bytes
|
|
|
132 |
*/
|
|
|
133 |
public function getFileSize()
|
|
|
134 |
{
|
|
|
135 |
return $this->_fileSize;
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
/**
|
|
|
139 |
* @return string the MIME-type of the uploaded file (such as "image/gif").
|
|
|
140 |
* This mime type is not checked on the server side and do not take its value for granted.
|
|
|
141 |
*/
|
|
|
142 |
public function getFileType()
|
|
|
143 |
{
|
|
|
144 |
return $this->_fileType;
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
/**
|
|
|
148 |
* @return string the local name of the file (where it is after being uploaded).
|
|
|
149 |
* Note, PHP will delete this file automatically after finishing this round of request.
|
|
|
150 |
*/
|
|
|
151 |
public function getLocalName()
|
|
|
152 |
{
|
|
|
153 |
return $this->_localName;
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
/**
|
|
|
157 |
* Returns an error code describing the status of this file uploading.
|
|
|
158 |
* @return integer the error code
|
|
|
159 |
* @see http://www.php.net/manual/en/features.file-upload.errors.php
|
|
|
160 |
*/
|
|
|
161 |
public function getErrorCode()
|
|
|
162 |
{
|
|
|
163 |
return $this->_errorCode;
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
/**
|
|
|
167 |
* @return boolean whether the file is uploaded successfully
|
|
|
168 |
*/
|
|
|
169 |
public function getHasFile()
|
|
|
170 |
{
|
|
|
171 |
return $this->_errorCode===UPLOAD_ERR_OK;
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
/**
|
|
|
175 |
* Saves the uploaded file.
|
|
|
176 |
* @param string the file name used to save the uploaded file
|
|
|
177 |
* @param boolean whether to delete the temporary file after saving.
|
|
|
178 |
* If true, you will not be able to save the uploaded file again.
|
|
|
179 |
* @return boolean true if the file saving is successful
|
|
|
180 |
*/
|
|
|
181 |
public function saveAs($fileName,$deleteTempFile=true)
|
|
|
182 |
{
|
|
|
183 |
if($this->_errorCode===UPLOAD_ERR_OK)
|
|
|
184 |
{
|
|
|
185 |
if($deleteTempFile)
|
|
|
186 |
return move_uploaded_file($this->_localName,$fileName);
|
|
|
187 |
else if(is_uploaded_file($this->_localName))
|
|
|
188 |
return file_put_contents($fileName,file_get_contents($this->_localName))!==false;
|
|
|
189 |
else
|
|
|
190 |
return false;
|
|
|
191 |
}
|
|
|
192 |
else
|
|
|
193 |
return false;
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
/**
|
|
|
197 |
* Loads user input data.
|
|
|
198 |
* This method is primarly used by framework developers.
|
|
|
199 |
* @param string the key that can be used to retrieve data from the input data collection
|
|
|
200 |
* @param array the input data collection
|
|
|
201 |
* @return boolean whether the data of the control has been changed
|
|
|
202 |
*/
|
|
|
203 |
public function loadPostData($key,$values)
|
|
|
204 |
{
|
|
|
205 |
if(isset($_FILES[$key]))
|
|
|
206 |
{
|
|
|
207 |
$this->_fileName=$_FILES[$key]['name'];
|
|
|
208 |
$this->_fileSize=$_FILES[$key]['size'];
|
|
|
209 |
$this->_fileType=$_FILES[$key]['type'];
|
|
|
210 |
$this->_errorCode=$_FILES[$key]['error'];
|
|
|
211 |
$this->_localName=$_FILES[$key]['tmp_name'];
|
|
|
212 |
return $this->_dataChanged=true;
|
|
|
213 |
}
|
|
|
214 |
else
|
|
|
215 |
return false;
|
|
|
216 |
}
|
|
|
217 |
|
|
|
218 |
/**
|
|
|
219 |
* Raises postdata changed event.
|
|
|
220 |
* This method calls {@link onFileUpload} method.
|
|
|
221 |
* This method is primarly used by framework developers.
|
|
|
222 |
*/
|
|
|
223 |
public function raisePostDataChangedEvent()
|
|
|
224 |
{
|
|
|
225 |
$this->onFileUpload(null);
|
|
|
226 |
}
|
|
|
227 |
|
|
|
228 |
/**
|
|
|
229 |
* This method is invoked when a file is uploaded during a postback.
|
|
|
230 |
* The method raises <b>OnFileUpload</b> event to fire up the event handler.
|
|
|
231 |
* If you override this method, be sure to call the parent implementation
|
|
|
232 |
* so that the event delegates can be invoked.
|
|
|
233 |
* @param TEventParameter event parameter to be passed to the event handlers
|
|
|
234 |
*/
|
|
|
235 |
public function onFileUpload($param)
|
|
|
236 |
{
|
|
|
237 |
$this->raiseEvent('OnFileUpload',$this,$param);
|
|
|
238 |
}
|
|
|
239 |
|
|
|
240 |
/**
|
|
|
241 |
* Returns a value indicating whether postback has caused the control data change.
|
|
|
242 |
* This method is required by the IPostBackDataHandler interface.
|
|
|
243 |
* @return boolean whether postback has caused the control data change. False if the page is not in postback mode.
|
|
|
244 |
*/
|
|
|
245 |
public function getDataChanged()
|
|
|
246 |
{
|
|
|
247 |
return $this->_dataChanged;
|
|
|
248 |
}
|
|
|
249 |
|
|
|
250 |
/**
|
|
|
251 |
* Returns the original file name as the property value to be validated.
|
|
|
252 |
* This method is required by IValidatable property.
|
|
|
253 |
* @return mixed the property value to be validated
|
|
|
254 |
*/
|
|
|
255 |
public function getValidationPropertyValue()
|
|
|
256 |
{
|
|
|
257 |
return $this->getFileName();
|
|
|
258 |
}
|
|
|
259 |
|
|
|
260 |
/**
|
|
|
261 |
* Returns true if this control validated successfully.
|
|
|
262 |
* Defaults to true.
|
|
|
263 |
* @return bool wether this control validated successfully.
|
|
|
264 |
*/
|
|
|
265 |
public function getIsValid()
|
|
|
266 |
{
|
|
|
267 |
return $this->_isValid;
|
|
|
268 |
}
|
|
|
269 |
/**
|
|
|
270 |
* @param bool wether this control is valid.
|
|
|
271 |
*/
|
|
|
272 |
public function setIsValid($value)
|
|
|
273 |
{
|
|
|
274 |
$this->_isValid=TPropertyValue::ensureBoolean($value);
|
|
|
275 |
}
|
|
|
276 |
|
|
|
277 |
}
|
|
|
278 |
|