A perfect php file Upload class instance

Source: Internet
Author: User
Tags php file upload

I can finally update my paperen website today ...... I am tired of writing my thesis, so I will post an article about the file upload class. This class is also written by myself. I don't know if it is useful to everyone, if you think you can, use it. However, the encoding is still not complete, and the data is submitted on the utf8 page. Therefore, if the generated file maintains the original Chinese name, the garbled name is displayed in the folder, if you do not want to see garbled characters, you need to perform the encoding conversion .?

The code is as follows: Copy code

<? Php

Define ('no _ file', 'Upload FILE does not exist ');
Define ('not _ ALLOW_EXT ',' the file type is NOT allowed ');
Define ('not _ ALLOW_SIZE ',' the file size is NOT within the permitted range ');
Define ('has _ THE_FILE ', 'This 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, 'title' => 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 is $ _ FILES.
The third parameter allows the file size to be measured in MB.
The fourth parameter allows jpg | png | gif
Does the fifth parameter need to generate a new file name?
The sixth parameter indicates that the returned message is in the format of 0. If 0 indicates that the upload is correct, 1 indicates that an array ('filename' => $ filename, 'filesize' => $ this-> _ filesize is returned, 'Tip '=> UPLOAD_SUCCESS );
***/

// Require ("class file ");
// $ U = new file_uploader;
// $ U-> ready ($ dir, $ _ FILES ['upload _ file'], false, false, true, 0 );
// Echo $ u-> do_upload ();

?>

Let's just talk about how to call this class. The method is very simple?

The code is as follows: Copy code

$ Dir = 'upload /';
Require ("upload_class.php ");
$ U = new file_uploader;
$ U-> ready ($ dir, $ _ FILES ['upload _ file'], false, false, true, 0 );
Echo $ u-> do_upload ();

Just like the hair-dyed advertisement that said N years ago, "left! Black hair is so easy !" In the same way, the operation of uploading files here is also "uploading files is so simple !". Of course, the $ _ FILES ['upload _ file'] is the name of the control corresponding to the file type in the form. If you do not need to output a prompt after the upload, you can view it on your own. For details about the parameters in the code, see here .?

Parameters of the ready method are described as follows:

The first parameter $ dir is the path for storing the uploaded file.
The second parameter is $ _ FILES.
The third parameter allows the file size to be measured in MB.
The fourth parameter allows jpg | png | gif
Does the fifth parameter need to generate a new file name?
The sixth parameter is the returned prompt format. 0 indicates that the upload is correct. 1. An array ('filename' => name of the uploaded file, 'filesize' => file size is returned, 'Tip '=> uploaded successfully );?

Good! So I completed an article, but it took a lot of time ......?

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.