This file upload class can customize the upload file size and upload file type and file save address, in the use of the process there is a problem is if the Chinese file name upload will appear garbled, so everyone solution is to save the page to Uft8 can be solved.
Here is the code for you to share:
<?phpdefine (' no_file ', ' no upload file ');d efine (' not_allow_ext ', ' file type is not in the allowed range ');d efine (' not_allow_size ', ' The file size is not in the allowed range ');d efine (' has_the_file ', ' The file already exists ');d efine (' upload_failed ', ' upload failed ');d efine (' upload_success ', ' upload succeeded '); Class file_uploader{var $_file; var $_filesize; var $_fileext; var $_filedir; var $_filename; var $_filetmpname; var $allowsize; var $allowext; var $neednewname; var $newname; var $syslang; var $report; function Ready ($filedir = ", $file, $allowsize =", $allowext = ", $neednewname = false, $report = 0) {$this->_fi Ledir = Is_dir ($filedir)? $filedir: "; if (Empty ($file) | | |!isset ($file [' size ']) | | $file [' size '] = = 0) $this->error (no_file); $this->_filesize = $file [' Size ']; $this->_filename = $file [' name ']; $this->_filetmpname = $file [' Tmp_name ']; $this->allowsize = $allowsize; $this->allowext = $allowext; $this->neednewname = ($neednewname)? True:false; $this->newname = "; $thiS->report = $report; } function Do_upload () {if (!is_uploaded_file ($this->_filetmpname)) $this->error (no_file); if ($this->chk_ext ()) {$this->error (not_allow_ext); Return '; } if ($this->chk_size ()) {$this->error (not_allow_size); Return '; if ($this->neednewname) $this->newname = $this->generate_name (). ".". $this->get_fileext (); if ($this->chk_hasfile ()) {$this->error (has_the_file); Return '; } $filename = Empty ($this->newname)? @iconv (' Utf-8 ', ' gb2312 ', $this->_filename): $this->newname; @chmod ($this->_filedir. $filename, 0777); if (Move_uploaded_file ($this->_filetmpname, $this->_filedir. $filename)) {return $this->result (); }else{$this->error (upload_failed); Return '; }} function Chk_ext () {if (Empty ($this->allowext) | | In_array ($this->get_fileext (), Explode ("|", $this->all Owext)) return false; return true; } functIon Chk_size () {if (Empty ($this->allowsize) | | get_filesize <= $this->allowsize*1024*1024) return false; return true; } function Get_filesize () {return $this->_filesize; } function Get_fileext () {return substr ($this->_filename,strrpos ($this->_filename, ".") +1); } function Generate_name () {return substr (MD5 (Time ()), 26); } function Chk_hasfile () {return is_file ($this->_filedir. $this->_filename); } function error ($tip) {echo $tip; } function result () {if ($this->report) {$filename = empty ($this->newname)? $this->_filename: $this-& Gt;newname; $arr = Array (' filename ' = = $filename, ' filesize ' = + $this->_filesize, ' tip ' = upload_success); return $arr; }else{return upload_success; }}/*** use method and parameter description ***//*** first parameter $dir the path to the upload file The second parameter is $_files for you. The third parameter of the upload file variable allows the file size unit to MB the fourth parameter allows the file type format to be jpg|png|g If the fifth parameter needs to generate a new file name, the sixth parameter is returned with the hint format 0 for direct prompt upload correctly 1 returns an array of arrays (' filename ' => $filename, ' filesize ' = $this->_filesize, ' tip ' = upload_success); ***///require ("Class file");//$u = new File_ uploader;//$u->ready ($dir, $_files[' Upload_file '), False, False, true, 0);//echo $u->do_upload ();? >
Calling methods
The code is as follows
$dir = ' upload/'; Require ("upload_class.php"); $u = new File_uploader; $u->ready ($dir, $_files[' Upload_file '), False, False, true, 0); echo $u->do_upload ();
add: in the code above still have some problem, oneself is in the UTF8 page to submit the data, so the generated file if keep the original Chinese name in the folder see is garbled name, if do not want to appear garbled words need to do a bit of code conversion.
The above is the whole content of this article, I hope that everyone's study has helped.