A complete php File Upload class example _ php instance

Source: Internet
Author: User
Tags php file upload
This article mainly introduces a complete PHP File Upload class instance, which can customize the file size, file type, and file storage address, you can refer to this file upload class to customize the file size, file type, and file storage address, there is a problem in the process of using the file name. If the file name is a Chinese character, garbled characters may occur. Therefore, you can 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.