This article mainly introduces a complete PHP File Upload class instance. You can customize the file size, file type, and file storage address. For more information, see
This article mainly introduces a complete PHP File Upload class instance. You can customize the file size, file type, and file storage address. For more information, see
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 error ($ 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-> _ filesize, 'ti P '=> 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 with encoding. You submitted the data on the utf8 page, therefore, if the generated file maintains the original Chinese name, the name is garbled in the folder. If you do not want to see garbled characters, you need to convert the code.
The above is all the content of this article, hoping to help you learn.