File upload class

xiaoxiao2021-03-06  47

$ UPLOAD_CLASS_ERROR = Array (1 => "does not allow uploading this format file ', 2 =>' directory cannot be written ', 3 =>' file already existing ', 4 =>' unknown error ', 5 =>' file too Big' );

/ *** purpose * file upload ** EXAMPLE * $ filer ['file'] = $ file; $ filer ['name'] = $ file_name; $ filer ['size'] = $ file_size; $ filer ['Type '] = $ file_type; // Allow uploaded file type $ fileTypes = array (' gif ',' jpg ',' jpge ',' png '); // File upload directory $ savePath = "/ usr / htdocs / Upload / "; // No maximum limit 0 Unlimited $ maxSize = 0; // Overlay 0 Not allowed 1 Allow $ overwrite = 0; $ UPLOAD = New Upload ($ Filearr, $ FILE_NAME, $ SavePath, $ FileTypes, $ Overwrite , $ maxSize); if (! $ upload-> run ()) {echo $ upload-> errmsg ();} ** @Author whXBB (Whxbb@21cn.com) * @version 0.1 * / class upload {var $ File; Var $ file_name; var $ file_size; var $ file_type;

/ ** Save the name * / var $ savename; / ** save the path * / var $ savepath; / ** File format Limited * / var $ fileformat = array (); / * Overwrite mode * / var $ overwrite = 0 ; / ** File maximum byte * / var $ maxSize = 0; / ** file extension * / var $ ext; / ** Error number * / var $ errno; / ** * Construction function * @Param $ filearr File information array 'file' temporary file location path and file name 'name' upload file name 'size' upload file size 'Type' upload file type * @Param Savename file save name * @Param SavePath file save path * @Param fileformat file Format Limit array * @Param overwriet Whether Overwrite 1 Allow overlay 0 Forbidden overlay * @Param MaxSize file largest size * / Function Upload ($ Filearr, $ Savename, $ SavePath, $ FILEFORMAT, $ OverWrite = 0, $ MAXSIZE = 0) { $ this-> file = $ filer ['file']; $ this-> file_name = $ filer ['name']; $ this-> file_size = $ filer ['size']; $ this-> file_type = $ filerR ['Type'];

$ this-> get_ext (); $ this-> set_savepath ($ savePath); $ this-> set_fileformat ($ fileformat); $ this-> set_overwrite ($ outrite); $ this-> set_savename ($ savename); $ this -> set_maxsize ($ maxsize);

/ ** Upload * / function run () {/ ** Check file format * / if (! $ This-> validate_format ()) {$ this-> errno = 1; return false;} / ** Check that the directory is Write * / if (! @ IS_WRITABLE) {$ this-> errno = 2; return false;} / ** Check if the file has existing * / if ($ this-> OverWrite == 0 && @file_exists ($ this-> savepath. $ this-> savename)) {$ this-> errno = 3; return false;} / ** If there is a size limit, check if the file exceeds the limit * / if ($ THIS-> MAXSI! = 0) {IF ($ this-> file_size> $ this-> maxsize) {$ this-> errno = 5; return false;}} / ** file upload * / if (! @ Copy ($ this-> file, $ this-> savepath. $ this-> savename)) {$ this-> errno = 4; return false;} / ** delete temporary files * / $ this-> Destory (); Return true;} / ** * File format check * @access protect * / function validate_for Mat () {

IF (! is_ARRAY ($ this-> fileformat) // No format restriction return true; $ ext = strtolower ($ this-> ext); reset ($ this-> fileformat); while (list ($ VAR, $ Key) ) = EACH ($ this-> fileformat) {IF ($ =) == $ ext) return true;} reset ($ this-> fileformat); Return False;}

/ ** * Get file extension * access public * / function get_ext () {$ ext = extra (".", $ This-> file_name); $ ext = extra xt [count ($ ext) - 1]; $ This-> ext = $ ext;} / ** * Set the maximum byte limit for upload file * @Param $ maxsize file size (bytes) 0: indicating unlimited * @access public * / function set_maxsize ($ maxsize) {$ $ this-> maxsize = $ maxSize;} / ** * Settings override mode * @Param overlay mode 1: Allow override 0: Prohibited overlay * @access public * / function set_overwrite ($ outrite) {$ this-> overwrite = $ overwrite }

/ ** * Set the file format allowed * @Param $ fileformat Allows uploaded file extensions * @access public * / function set_fileformat ($ fileformat) {$ this-> fileformat = $ fileformat;}

/ ** * Set the save path * @Param $ savePath file save the path: "/" end * @access public * / function set_savepath ($ savePath) {$ this-> savepath = savepath;} / ** * set file Save Name * @SAVENAME Save Name, if you are empty, then the system automatically generates a random file name * @access public * / function set_savename ($ savename) {if ($ savename == ') // If the file name is not set Then generate a random file name {SRAND ((Double) microtime () * 1000000); $ RND = RAND (100,999); $ name = Date ('u') $ RND; $ name = $ name. "." . $ this-> ext;} else {$ name = $ savename;} $ this-> savename = $ name;} / ** * Delete file * @param $ file to delete file name * @access public * / function Del ($ file) {if (! @ unlink ($ file)) {$ this-> errno = 3; return false;} return true;} / ** * Delete temporary file * @access proCtect * / function destory () {$ This-> del ($ this-> file);} / ** * get an error message * @access public * @retu Rn error msg string or false * / function errmsg () {global $ upload_class_error; if ($ this-> errno == 0) Return False; Else Return $ upload_class_error [$ this-> errno]}}?>

转载请注明原文地址:https://www.9cbs.com/read-110231.html

New Post(0)