5
* a smart class help you in checking and saving uploads
6
* Visit http://en.vietapi.com/wiki/index.php/PHP:_HttpUpload for more information
8
* @author Nguyen Quoc Bao <quocbao.coder@gmail.com>
12
define('HTTPUPLOAD_ERROR_OK' , 1);
13
define('HTTPUPLOAD_ERROR_NO_FILE' , -1);
14
define('HTTPUPLOAD_ERROR_INI_SIZE' , -2); //php size limit
15
define('HTTPUPLOAD_ERROR_FORM_SIZE' , -3); //form size limit
16
define('HTTPUPLOAD_ERROR_SIZE' , -4); //class size limit
17
define('HTTPUPLOAD_ERROR_IMG' , -5); //image size limit
18
define('HTTPUPLOAD_ERROR_EXT' , -6); //extension is not allowed
19
define('HTTPUPLOAD_ERROR_MIME' , -7); //mime is not allowed
20
define('HTTPUPLOAD_ERROR_WRITE' , -8); //there was a problem during processing file
21
define('HTTPUPLOAD_ERROR_PARTIAL' , -9); //The uploaded file was only partially uploaded
27
var $uploadIndex = '';
31
var $handlerType = ''; //move , copy , data
36
var $allowExt = array();
37
var $allowMime = array();
38
var $fileCHMOD = 0777;
40
var $extension = false;
42
var $error_lang = array(
43
HTTPUPLOAD_ERROR_NO_FILE => "No file was submited.",
44
HTTPUPLOAD_ERROR_INI_SIZE => "The uploaded file exceeds the upload_max_filesize directive in php.ini.",
45
HTTPUPLOAD_ERROR_FORM_SIZE => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.",
46
HTTPUPLOAD_ERROR_SIZE => "The uploaded file exceeds the max file size directive that was specified in class setting.",
47
HTTPUPLOAD_ERROR_IMG => "The uploaded file exceeds the max image width or max image height directive that was specified in class setting.",
48
HTTPUPLOAD_ERROR_EXT => "File extension is not allowed.",
49
HTTPUPLOAD_ERROR_MIME => "File mime is not allowed.",
50
HTTPUPLOAD_ERROR_WRITE => "There was an error writing the file on this system. Maybe the file name you're trying to upload already exists, or your webserver cannot write to the file or directory. This is likely a permission problem - make sure the webserver can write to the upload directory..",
51
HTTPUPLOAD_ERROR_PARTIAL => "The uploaded file was only partially uploaded.",
55
* Set upload directory
56
* @param string $dir upload directory
58
function setuploaddir($dir) {
59
$this->uploadDir = $dir;
64
* @param string $name $HTTP_POST_VARS[$name]
66
function setuploadname($name) {
67
$this->uploadName = $name;
71
* Set upload name index
72
* @param string $name $HTTP_POST_VARS[$name][$index]
74
function setuploadindex($index) {
75
$this->uploadIndex = $index;
80
* @param string $name File name
82
function settargetfile($name) {
83
$this->targetName = $name;
87
* Set upload image size
88
* @param string $w Max image width
89
* @param string $h Max image height
91
function setimagemaxsize($w = null , $h = null) {
93
$this->maxHeight = $h;
97
* Set upload mime filter
98
* @param mixed $a Filter data
99
* @param string $s Text seperator (for string filter data)
101
function setallowmime($a , $s = '|') {
103
if (strpos($a , $s) === false) {
106
$a = explode($s , $a);
109
$this->allowMime = array();
110
foreach ($a as $val) {
111
$this->allowMime[] = strtolower(trim($val));
116
* Set upload extension filter
117
* @param mixed $a Filter data
118
* @param string $s Text seperator (for string filter data)
120
function setallowext($a , $s = '|') {
122
if (strpos($a , $s) === false) {
125
$a = explode($s , $a);
128
$this->allowExt = array();
129
foreach ($a as $val) {
131
if (substr($val , 0 , 1) != ".") $val = ".$val";
132
$this->allowExt[] = strtolower($val);
139
* @param int $size Max file size
141
function setmaxsize($size) {
142
$this->maxSize = intval($size);
145
function httpupload($dir = '' , $name = '' , $index = '') {
146
$this->uploadDir = $dir;
147
$this->uploadName = $name;
148
$this->uploadIndex = $index;
151
function getuploadname() {
152
$FILE = $this->getuploadinfo($this->uploadName , $this->uploadIndex);
153
if ($FILE === false) return false;
154
return @$FILE['name'];
157
function getuploadsize() {
158
$FILE = $this->getuploadinfo($this->uploadName , $this->uploadIndex);
159
if ($FILE === false) return false;
160
return @$FILE['size'];
163
function getuploadtype() {
164
$FILE = $this->getuploadinfo($this->uploadName , $this->uploadIndex);
165
if ($FILE === false) return false;
166
return @$FILE['type'];
169
function getuploadtmp() {
170
$FILE = $this->getuploadinfo($this->uploadName , $this->uploadIndex);
171
if ($FILE === false) return false;
172
return @$FILE['tmp_name'];
175
function getsavedname($fullpath=true) {
176
$FILE = $this->getuploadinfo($this->uploadName , $this->uploadIndex);
177
if ($FILE === false || $this->savedName == '') return false;
178
return ($fullpath ? $this->uploadDir . "/" : "") . $this->savedName;
182
$FILE = $this->getuploadinfo($this->uploadName , $this->uploadIndex);
183
if ($FILE === false) return true;
184
return ($FILE['size'] == 0);
187
function hasupload() {
188
$FILE = $this->getuploadinfo($this->uploadName , $this->uploadIndex);
189
if ($FILE === false) return false;
190
return (isset($FILE['name']));
194
* Default file upload handler
197
function processfile($b , $t , $mod , $overWrite = false) {
198
$FILE = $this->getuploadinfo($this->uploadName , $this->uploadIndex);
199
if ($FILE === false) return false;
201
$p2 = $FILE['tmp_name'];
202
if (trim($b) == '') {
205
if (substr($b , strlen($b) - 1 , 1) != $this->seperator) $p = $b . $this->seperator;
209
if (file_exists($p)) {
210
//exist file , have to check
211
if (is_dir($p)) return false;
212
if (!$overWrite) return false;
214
if (!@copy($p2 , $p)) return false;
216
$this->savedName = $t;
221
* Check and Save upload file
222
* @param bool $overWrite Overwrite existed file
225
function upload($overWrite = false) {
226
$this->savedName = '';
228
if (!$this->hasUpload()) return $this->set_error(HTTPUPLOAD_ERROR_NO_FILE);
229
$FILE = $this->getuploadinfo($this->uploadName , $this->uploadIndex);
230
switch ($FILE['error']) {
232
return $this->set_error(HTTPUPLOAD_ERROR_INI_SIZE);
235
return $this->set_error(HTTPUPLOAD_ERROR_FORM_SIZE);
238
return $this->set_error(HTTPUPLOAD_ERROR_PARTIAL);
241
return $this->set_error(HTTPUPLOAD_ERROR_NO_FILE);
245
if (!(strpos($FILE['name'] , ".")) === false) {
246
$ext = explode("." , $FILE['name']);
247
$ext = "." . $ext[count($ext) - 1];
249
$ext = strtolower($ext);
250
//check max file size
251
if (intval($this->maxSize) > 0 && $FILE['size'] > $this->maxSize) return $this->set_error(HTTPUPLOAD_ERROR_SIZE);
253
if (is_array($this->allowExt) && count($this->allowExt) > 0 && !in_array($ext , $this->allowExt)) return $this->set_error(HTTPUPLOAD_ERROR_EXT);
255
if (is_array($this->allowMime) && count($this->allowMime) > 0 && !in_array($FILE['type'] , $this->allowMime)) return $this->set_error(HTTPUPLOAD_ERROR_MIME);
257
if (intval($this->maxWidth) > 0 || intval($this->maxHeight) > 0) {
258
$imageSize = @getimagesize($FILE['tmp_name']);
259
if ($imageSize === false) return false;
260
if (intval($this->maxWidth) > 0 && $imageSize[0] > intval($this->maxWidth)) return $this->set_error(HTTPUPLOAD_ERROR_IMG);
261
if (intval($this->maxHeight) > 0 && $imageSize[1] > intval($this->maxHeight)) return $this->set_error(HTTPUPLOAD_ERROR_IMG);
264
if (trim($this->targetName) == '') {
266
if ($this->prefix === false) {
268
if (!(strpos($f , ".") === false)) {
269
$f = explode("." , $f);
270
unset($f[count($f) -1]);
271
$f = implode("." , $f);
274
$f = uniqid(trim($this->prefix));
276
if ($this->extension === false) {
277
if ($ext != '.') $f .= $ext;
279
if (substr($this->extension , 0 , 1) != ".") $this->extension = "." . $this->extension;
280
if (trim($this->extension) != '.') $f .= $this->extension;
284
$f = trim($this->targetName);
286
$this->savedName = $f;
287
//ok , now process copy
288
if ($this->handlerType == '' || $this->handler == '') {
289
//process default handler
290
if ($this->processfile($this->uploadDir , $f , $this->fileCHMOD , $overWrite)) return $this->set_error(HTTPUPLOAD_ERROR_OK);
291
else return $this->set_error(HTTPUPLOAD_ERROR_WRITE);
293
//process user handler
294
$b = $this->uploadDir;
295
if (trim($b) == '') {
298
if (substr($b , strlen($b) - 1 , 1) != $this->seperator) $p = $b . $this->seperator;
303
switch (trim(strtolower($this->handlerType))) {
306
// function (src , tgt , CHMOD)
309
return $f[0]->$f[1]($p , $FILE['tmp_name'] , $this->fileCHMOD);
311
else return $f($p , $FILE['tmp_name'] , $this->fileCHMOD);
313
if (@call_user_func($f , $p , $FILE['tmp_name'] , $this->fileCHMOD)) return $this->set_error(HTTPUPLOAD_ERROR_OK);
314
else return $this->set_error(HTTPUPLOAD_ERROR_WRITE);
317
// function (targetFile , data , CHMOD)
319
if (is_readable($FILE['tmp_name'])) $data = implode("" , file($FILE['tmp_name']));
322
return $f[0]->$f[1]($p , $data , $this->fileCHMOD);
324
else return $f($p , $data , $this->fileCHMOD);
326
if (@call_user_func($f , $p , $data , $this->fileCHMOD)) return $this->set_error(HTTPUPLOAD_ERROR_OK);
327
else return $this->set_error(HTTPUPLOAD_ERROR_WRITE);
330
if ($this->processfile($this->uploadDir , $f , $this->fileCHMOD , $overWrite)) return $this->set_error(HTTPUPLOAD_ERROR_OK);
331
else return $this->set_error(HTTPUPLOAD_ERROR_WRITE);
337
function upload_ex($name,$overwrite=false) {
338
$FILES = $this->getuploadinfo($name,null);
339
if (isset($FILES['name']) && is_array($FILES['name'])) {
341
$old_index = $this->uploadIndex;
342
$old_name = $this->uploadName;
344
$this->uploadName = $name;
346
foreach ($FILES['name'] as $index => $dummy) {
348
$this->uploadIndex = $index;
349
$this->upload($overwrite);
352
'error_code' => $this->error_code ,
353
'name' => $this->getuploadname() ,
354
'size' => $this->getuploadsize() ,
355
'type' => $this->getuploadtype() ,
356
'tmp_name' => $this->getuploadtmp() ,
357
'file' => $this->getsavedname(false) ,
358
'fullpath' => $this->getsavedname(true) ,
362
$this->uploadIndex = $old_index;
363
$this->uploadName = $old_name;
368
function &getuploadinfo($name , $index = '') {
369
global $HTTP_POST_FILES;
370
if ($index == '' && !($index === 0)) {
371
if (isset($HTTP_POST_FILES[$name])) {
372
return $HTTP_POST_FILES[$name];
375
if (isset($HTTP_POST_FILES[$name]['name'][$index])) {
377
'name' => $HTTP_POST_FILES[$name]['name'][$index],
378
'tmp_name' => $HTTP_POST_FILES[$name]['tmp_name'][$index],
379
'size' => $HTTP_POST_FILES[$name]['size'][$index],
380
'type' => $HTTP_POST_FILES[$name]['type'][$index],
381
'error' => $HTTP_POST_FILES[$name]['error'][$index],
388
function set_error($code) {
389
$this->error_code = $code;
390
if ($code != HTTPUPLOAD_ERROR_OK) return false;
394
function get_error($code = null) {
395
if ($code === null) $code = $this->error_code;
396
return @$this->error_lang[$code];
b'\\ No newline at end of file'