This file upload class can be customized 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 into a uft8 can be resolved.
Here's the code to share:
<?php define (' No_file ', ' No uploading file ');
Define (' Not_allow_ext ', ' file type not allowed ');
Define (' Not_allow_size ', ' File size not allowed ');
Define (' Has_the_file ', ' The document already exists ');
Define (' upload_failed ', ' upload failed ');
Define (' upload_success ', ' upload success ');
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->_f Iledir = 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;
The 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 "; The 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->allowext)))
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: $th
is->newname;
$arr = Array (' filename ' => $filename, ' filesize ' => $this->_filesize, ' Tip ' => upload_success);
return $arr;
}else{return upload_success; /*** use method and parameter descriptions ***//*** the first parameter $dir the path for the uploaded file The second parameter is $_files for you that upload file variable The third parameter allows the fileSize unit MB Fourth parameter allowed file type format is jpg|png|gif fifth parameter whether need to generate new file name sixth parameter to return prompt format 0 for direct upload correct 1 returns an array of arrays (' filename ' => $file
Name, ' filesize ' => $this->_filesize, ' Tip ' => upload_success);
///require ("Class document");
$u = new File_uploader;
$u->ready ($dir, $_files[' Upload_file '), False, False, true, 0); echo $u->do_upload ();?>
Call method
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 ();
Supplementary Note: in the code above or some problems, they are in the utf8 of the page will be submitted to the data, so the resulting file if the original Chinese name in the folder to see is garbled name, if you do not want to appear garbled words need to do a code conversion.
The above is the entire content of this article, I hope to help you learn.