1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
| <?php class Upload{ var $saveName; var $savePath; var $fileFormat = array('gif','jpg','doc','application/octet-stream'); var $overwrite = 0; var $maxSize = 0; var $ext; var $thumb = 0; var $thumbWidth = 130; var $thumbHeight = 130; var $thumbPrefix = "_thumb_"; var $errno; var $returnArray= array(); var $returninfo= array();
function Upload($savePath, $fileFormat='',$maxSize = 0, $overwrite = 0) { $this->setSavepath($savePath); $this->setFileformat($fileFormat); $this->setMaxsize($maxSize); $this->setOverwrite($overwrite); $this->setThumb($this->thumb, $this->thumbWidth,$this->thumbHeight); $this->errno = 0; }
function run($fileInput,$changeName = 1){ if(isset($_FILES[$fileInput])){ $fileArr = $_FILES[$fileInput]; if(is_array($fileArr['name'])){ for($i = 0; $i < count($fileArr['name']); $i++){ $ar['tmp_name'] = $fileArr['tmp_name'][$i]; $ar['name'] = $fileArr['name'][$i]; $ar['type'] = $fileArr['type'][$i]; $ar['size'] = $fileArr['size'][$i]; $ar['error'] = $fileArr['error'][$i]; $this->getExt($ar['name']); $this->setSavename($changeName == 1 ? '' : $ar['name']); if($this->copyfile($ar)){ $this->returnArray[] = $this->returninfo; }else{ $this->returninfo['error'] = $this->errmsg(); $this->returnArray[] = $this->returninfo; } } return $this->errno ? false : true; }else{ $this->getExt($fileArr['name']); $this->setSavename($changeName == 1 ? '' : $fileArr['name']); if($this->copyfile($fileArr)){ $this->returnArray[] = $this->returninfo; }else{ $this->returninfo['error'] = $this->errmsg(); $this->returnArray[] = $this->returninfo; } return $this->errno ? false : true; } return false; }else{ $this->errno = 10; return false; } }
function copyfile($fileArray){ $this->returninfo = array(); $this->returninfo['name'] = $fileArray['name']; $this->returninfo['md5'] = @md5_file($fileArray['tmp_name']); $this->returninfo['saveName'] = $this->saveName; $this->returninfo['size'] = number_format( ($fileArray['size'])/1024 , 0, '.', ' '); $this->returninfo['type'] = $fileArray['type']; if (!$this->validateFormat()){ $this->errno = 11; return false; } if(!@is_writable($this->savePath)){ $this->errno = 12; return false; } if ($this->maxSize != 0 ){ if ($fileArray["size"] > $this->maxSize){ $this->errno = 14; return false; } } if(!@move_uploaded_file($fileArray["tmp_name"], $this->savePath.$this->saveName)){ $this->errno = $fileArray["error"]; return false; }elseif( $this->thumb ){ $CreateFunction = "imagecreatefrom".($this->ext == 'jpg' ? 'jpeg' : $this->ext); $SaveFunction = "image".($this->ext == 'jpg' ? 'jpeg' : $this->ext); if (strtolower($CreateFunction) == "imagecreatefromgif" && !function_exists("imagecreatefromgif")) { $this->errno = 16; return false; } elseif (strtolower($CreateFunction) == "imagecreatefromjpeg" && !function_exists("imagecreatefromjpeg")) { $this->errno = 17; return false; } elseif (!function_exists($CreateFunction)) { $this->errno = 18; return false; } $Original = @$CreateFunction($this->savePath.$this->saveName); if (!$Original) {$this->errno = 19; return false;} $originalHeight = ImageSY($Original); $originalWidth = ImageSX($Original); $this->returninfo['originalHeight'] = $originalHeight; $this->returninfo['originalWidth'] = $originalWidth;
$radio=max(($originalWidth/$this->thumbWidth),($originalHeight/$this->thumbHeight)); $thumbWidth=(int)$originalWidth/$radio; $thumbHeight=(int)$originalHeight/$radio; if ($thumbWidth == 0) $thumbWidth = 1; if ($thumbHeight == 0) $thumbHeight = 1; $createdThumb = imagecreatetruecolor($thumbWidth, $thumbHeight); if ( !$createdThumb ) {$this->errno = 20; return false;} if ( !imagecopyresampled($createdThumb, $Original, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $originalWidth, $originalHeight) ) {$this->errno = 21; return false;} if ( !$SaveFunction($createdThumb, $this->savePath.$this->thumbPrefix.$this->saveName) ) {$this->errno = 22; return false;} }
return true; }
function validateFormat(){ if(!is_array($this->fileFormat) || in_array(strtolower($this->ext), $this->fileFormat) || in_array(strtolower($this->returninfo['type']), $this->fileFormat) ) return true; else return false; }
function getExt($fileName){ $ext = explode(".", $fileName); $ext = $ext[count($ext) - 1]; $this->ext = strtolower($ext); }
function setMaxsize($maxSize){ $this->maxSize = $maxSize; }
function setFileformat($fileFormat){ if(is_array($fileFormat)){$this->fileFormat = $fileFormat ;} }
function setOverwrite($overwrite){ $this->overwrite = $overwrite; }
function setSavepath($savePath){ $this->savePath = substr( str_replace("\\","/", $savePath) , -1) == "/" ? $savePath : $savePath."/"; }
function setThumb($thumb, $thumbWidth = 0,$thumbHeight = 0){ $this->thumb = $thumb; if($thumbWidth) $this->thumbWidth = $thumbWidth; if($thumbHeight) $this->thumbHeight = $thumbHeight; }
function setSavename($saveName){ if ($saveName == ''){ $name = date('YmdHis')."_".rand(100,999).'.'.$this->ext; if(file_exists($this->savePath . $name)){ $name = setSavename($saveName); } } else { $name = $saveName; } $this->saveName = $name; }
function del($fileName){ if(!@unlink($fileName)){ $this->errno = 15; return false; } return true; }
function getInfo(){ return $this->returnArray; }
function errmsg(){ $uploadClassError = array( 0 =>'There is no error, the file uploaded with success. ', 1 =>'The uploaded file exceeds the upload_max_filesize directive in php.ini.', 2 =>'The uploaded file exceeds the MAX_FILE_SIZE that was specified in the HTML form.', 3 =>'The uploaded file was only partially uploaded. ', 4 =>'No file was uploaded. ', 6 =>'Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3. ', 7 =>'Failed to write file to disk. Introduced in PHP 5.1.0. ', 10 =>'Input name is not unavailable!', 11 =>'The uploaded file is Unallowable!', 12 =>'Directory unwritable!', 13 =>'File exist already!', 14 =>'File is too big!', 15 =>'Delete file unsuccessfully!', 16 =>'Your version of PHP does not appear to have GIF thumbnailing support.', 17 =>'Your version of PHP does not appear to have JPEG thumbnailing support.', 18 =>'Your version of PHP does not appear to have pictures thumbnailing support.', 19 =>'An error occurred while attempting to copy the source image . Your version of php ('.phpversion().') may not have this image type support.', 20 =>'An error occurred while attempting to create a new image.', 21 =>'An error occurred while copying the source image to the thumbnail image.', 22 =>'An error occurred while saving the thumbnail image to the filesystem. Are you sure that PHP has been configured with both read and write access on this folder?', ); if ($this->errno == 0) return false; else return $uploadClassError[$this->errno]; } } ?>
|