A complete PHP file Upload class example ,. An example of a complete PHP file Upload class. This File upload class can customize the file size, file type, and file storage address, one problem during use is a complete PHP file Upload class instance explanation,
This file Upload class allows you to customize the file size, file type, and file storage address. a problem occurs when you use a Chinese file name to upload files, the solution is to save the page as uft8.
The following code is shared with you:
<? Phpdefine ('no _ file', 'file NOT upload'); define ('not _ ALLOW_EXT ', 'File type NOT allowed '); define ('not _ ALLOW_SIZE ',' the file size is NOT within the permitted range '); define ('has _ THE_FILE', 'The file already exists '); define ('upload _ failed', 'Upload failed'); define ('upload _ SUCCESS ', 'upload successful'); 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-> _ filedir = 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-> 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 e Rror ($ tip) {echo $ tip;} function result () {if ($ this-> report) {$ filename = empty ($ this-> newname )? $ This-> _ filename: $ this-> newname; $ arr = array ('filename' => $ filename, 'filesize' => $ this-> _ filesize, 'Tip '=> UPLOAD_SUCCESS); return $ arr;} else {return UPLOAD_SUCCESS ;}}} /*** usage and parameter description *** // *** The first parameter $ dir is the path for storing the uploaded file. The second parameter $ _ FILES is your uploaded file variable. the third parameter allows the file size unit to MB. The fourth parameter allows the file type format to be jpg | png | whether the fifth parameter of gif needs to generate a new file name. The sixth parameter is the returned prompt format 0. returns an array ('filename' => $ filename, 'filesize' => $ this-> _ files Ize, 'tip '=> UPLOAD_SUCCESS); *** // require ("class file"); // $ 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();
Note:There are still some problems in coding. I submitted the data on the utf8 page. Therefore, if the generated file maintains the original Chinese name, the garbled name will be displayed in the folder, if you do not want to see garbled characters, you need to perform the encoding conversion.
The above is all the content of this article, hoping to help you learn.
Upload, this file Upload class can customize the size of the uploaded file, the type of the uploaded file, and the file storage address. a problem occurs during use...