Very classic PHP File upload class sharing,
File Upload is a common feature in project development, but the file upload process is cumbersome, as long as there is a file upload where you need to write these complex code. In order to reduce the difficulty of writing functionality in each development, and in order to save development time, we typically encapsulate these reusable snippets into a class.
<?php/** +-----------------------------------------------------------------------------* File Upload class +-------------- ---------------------------------------------------------------* @author Administrator +------------------------- ----------------------------------------------------*/class fileupload{private $filepath; Save path Private $allowtype =array (' gif ', ' jpg ', ' jpeg ', ' png ', ' txt '); Private $maxsize = 1000000; Maximum allowed upload size private $israndname =true; Whether the random private $orginame; Original file name private $tmpname; Temporary file name private $newname; New file name private $filetype; File type private $filesize; File size Private $errornum = '; The error number is private $errormsg; Error message/** +------------------------------------------------------------------------------* constructor +---------------- --------------------------------------------------------------* @param string $savepath Save path * @param string $allowtyp e Allow type * @param string $maxsize Allow size * +------------------------------------------------------------------------------*/function __construct ($option =array ()) {foreach ($option as $key = $value) {if (!in_array ($key, Get_class_vars (Get_class ($this)))) {continue; } $this->setoption ($key, $value); }} function UploadFile ($field) {$return =true; if (! $this->checkpath ()) {$this->errormsg= $this->geterrornum (); return false; } $name =$_files[$field [' name ']; $tmpname =$_files[$field [' Tmp_name ']; $filesize =$_files[$field [' size ']; $error =$_files[$field] [' ERROR ']; if (Is_array ($name)) {$errors =array (); for ($i =0; $i
GetFile ($name [$i], $tmpname [$i], $filesize [$i], $errors [$i]) {if (! $this->checksize () &&! $this->c Hecktype ()) {$errors = $this->geterrornum (); return false; }}else{$errors = $this->geterrornum (); return false; } if (! $return) {$this->getfile (); }} if ($return) {$fileNames =array (); for ($i =0; $i
GetFile ($name [$i], $tmpname [$i], $filesize [$i], $filesize [$i]) {$this->setfilename (); if (! $this->movefile ()) {$errors []= $this->geterrornum (); $return =false; }else{$fileNames []= $this->getnewname (); }}} $this->newname= $fileNames; } $this->errormsg= $errors; return $return; }else{if ($this->getfile ($name, $tmpname, FileSize, $error)) {if (! $this->checksize ()) {return False } if (! $this->checktype ()) {return false; } $this->setfilename (); if ($this->movefile ()) {return true; }}else{return false; } if (! $return) {$this->setoption (' ErrorNum ', 0); $this->errormsg= $this->geterrornum (); } return $return; } } /** +------------------------------------------------------------------------* Set Class Property value function +--------------------------------------------- ---------------------------* @param Mix $key * @param mix $value */Private Function setOption ($key, $value) { $key =strtolower ($key); $this $key = $value; }/** +---------------------------------------------------------------------------* Get file variable parameter function +-------------- -------------------------------------------------------------* @param string $name * @param string $tmp _name * @ Param number $size * @param number $error */Private Function GetFile ($name, $tmpname, $filetype, $filesize, $error =0) {$this->setoption (' Tmpname ', $tmpname); $this->setoption (' Orginame ', $name); $arrstr =explode ('. ', $name); $this->setoption (' FileType ', $arrstr [count ($ARRSTR)-1]); $this->setoption (' FileSize ', $filesize); return true; } /** +-------------------------------------------------------------------------* Check the upload path function +-------------------------------------------------------------------------* @return bo Olean */Private Function Checkpath () {if (Empty ($this->filepath)) {$this->setoption (' ErrorNum ',-5); return false; } if (!file_exists ($this->filepath) | |! Is_writable ($this->filepath)) {if (! @mkdir ($this->filepath,0755)) {$this->setoption (' ErrorNum '), -4); return false; }} return true; } Private Function Is_http_post () {if (!is_uploaded_file ($this->tmpname)) {$this->setoption (' ErrorNum ', -6); return false; }else{return true; }}/** +--------------------------------------------------------------------* Check file size function +-------------------- ------------------------------------------------* @return Boolean */Private Function checksize () {if ($this- >filesize> $this->maxsize) {$this->setoption (' ErrorNum ',-2); return false; }else{return true; }}/** +---------------------------------------------------------------* Check file type function +------------------------ ---------------------------------------* @return Boolean */Private Function Checktype () {if (In_array ($this- >filetype, $this->allowtype)) {return true; }else{$this->setoption (' ErrorNum ',-1); return false; }} Private Function Setfilename () {if ($this->israndname) {$this->setoption (' NewName ', $this->ran Dname ()); }else{$this->setoption (' NewName ', $this->orginame); }}/** +-----------------------------------------------------------------* Get new file name +----------------------- -------------------------------------------*/Public Function Getnewname () {return $this->newname; } Private Function Randname () {$rule =date ("Ymdhis"). Rand (0, 999); Return $rule. '. '. $this->filetype; } privAte function MoveFile () {if ($this->errornum) {$filepath =rtrim ($this->filaepath, '/'). ' /'; $filepath. = $this->newname; if (@move_uploaded_file ($this->tmpname, $filepath)) {return true; }else{$this->errormsg= $this->setoption (' ErrorNum ',-3); }}else{return false; }}/** +----------------------------------------------------------------* error message function +------------------------- ---------------------------------------* @return String */function Geterrornum () {$erstr = "Upload file {$this->o Rginame} error "; Switch ($this->errornum) {case 4: $erstr. = "No files were uploaded"; Break Case 3: $erstr. = "file is only partially uploaded"; Break Case 2: $erstr. = "The uploaded file exceeds the value specified by the HTML form max_file_size"; Break Case 1: $erstr. = "The uploaded file exceeds the Upload_max_filesize value in the php.ini configuration file"; Break Case 0: $erstr = "Upload {$this->orginame} succeeded"; BreakCase-1: $erstr = "disallowed type"; Break Case-2: $erstr. = "File is too large to exceed {$this->maxsize} bytes"; Break Case-3: $erstr. = "Upload Failed"; Break Case-4: $erstr = "Failed to create the upload directory, please re-specify the upload directory"; Break Case-5: $erstr = "No upload path specified"; Break Case-6: $erstr = "illegal operation"; Break Default: $erstr. = "Unknown error"; } return $ERSTR; }}?>
The above is the whole content of this article, I hope that you learn PHP programming help.
http://www.bkjia.com/PHPjc/1127897.html www.bkjia.com true http://www.bkjia.com/PHPjc/1127897.html techarticle Very classic PHP file upload class sharing, file upload is a common feature in project development, but the process of file upload is more cumbersome, as long as there is a file upload where you need to make up ...