Php file Upload class _ PHP Tutorial

Source: Internet
Author: User
Tags html form input php file upload
The file Upload class used in php. Classupload {*** name attribute of the input field of the html form. the default attribute is file * var $ file_fieldfile; *** $ _ files array * var $ file_array; *** save path, the default path is the current path.
Class upload {
/**
* Name attribute of the input field of an html form. the default value is 'file'
*/
Var $ file_field = 'file ';

/**
* $ _ Files array
*/
Var $ file_array;

/**
* Save path. the default value is the current path.
*/
Var $ save_path = '';

/**
* Custom file name
*/
Var $ define_name;

/**
* Final file name
*/
Var $ name;

/**
* File size, in bytes
*/
Var $ size;

/**
* File extension, excluding "."
*/
Var $ ext;

/**
* The file type that can be uploaded. the default value is unlimited.
*/
Var $ allow_ext = array ();

/**
* The file type that can be uploaded. the default value is unlimited.
*/
Var $ allow_size = false;

/**
* If a file with the same name already exists, whether to overwrite the file. by default, the file is not overwritten.
*/
Var $ overwrite = false;

/**
* Error message
*/
Var $ error_code;

/**
* Constructor
*/
Public function _ construct (){
If (! Is_uploaded_file ($ _ files [$ this-> file_field] ['tmp _ name']) {
Die ("illegal Upload! ");
} Else {
$ This-> file_array = $ _ files [$ this-> file_field];
$ This-> name = $ this-> getpro ('name ');
$ This-> size = $ this-> getpro ('size ');
$ This-> ext = $ this-> getpro ('text ');
}
}


/**
* Upload operation functions
* @ Return true if the abstract object is uploaded successfully. Otherwise, the corresponding error code is returned.
* @ Return string or bool
*/
Public function upload (){
If (is_uploaded_file ($ this-> file_array ['tmp _ name']) {

If (! Empty ($ this-> allow_ext )){
If (! In_array ($ this-> ext, $ this-> allow_ext )){
$ This-> error_code = "this type of file cannot be uploaded! ";
Return false;
}
}

If (! @ File_exists (iconv ('utf-8', 'gbk', $ this-> save_path ))){
$ This-> error_code = "the file upload directory does not exist! ";
Return false;
}

If (! Is_writable (iconv ('utf-8', 'gbk', $ this-> save_path ))){
$ This-> error_code = "the file upload directory cannot be written! ";
Return false;
}

If ($ this-> overwrite = false & @ file_exists (iconv ('utf-8', 'gbk', $ this-> save_path. $ this-> name ))){
$ This-> error_code = "this file already exists! ";
Return false;
}

If ($ this-> allow_size ){
If ($ this-> size> $ this-> allow_size ){
$ This-> error_code = "the file size exceeds the system limit! ";
Return false;
}
}

$ Result = @ move_uploaded_file ($ this-> file_array ['tmp _ name'], iconv ("UTF-8", "gbk", $ this-> save_path. $ this-> getpro ("name ")));
If ($ result ){
Return true;
} Else {
Switch ($ this-> file_array ['error']) {
Case 1:
$ This-> error_code = "the uploaded file exceeds the limit of the upload_max_filesize option! ";
Break;
Case 2:
$ This-> error_code = "the size of the uploaded file exceeds the value specified by the max_file_size option! ";
Break;
Case 3:
$ This-> error_code = "the upload process is interrupted! ";
Break;
Case 4:
$ This-> error_code = "no file is uploaded! ";
Break;
Case 6:
$ This-> error_code = "The temporary folder cannot be found! ";
Break;
Case 7:
$ This-> error_code = "an error occurred while writing the file! ";
Break;
}
Return false;
}
}
}

/**
* Upload operation functions
* @ Abstract get file attributes
* @ Param $ itme: string type. Valid input values include name (file name), ext (file extension), and size (file size)
* @ Return string
*/
Public function getpro ($ item ){
Switch ($ item ){
Case "name ":
$ Filename = $ this-> file_array ['name'];
Return isset ($ this-> define_name )? $ This-> define_name. ".". $ this-> ext: $ filename;
Break;
Case "ext ":
$ Filename = $ this-> file_array ['name'];
$ Filter = explode (".", $ filename );
Return strtolower ($ filter [count ($ filter)-1]);
Break;
Case "size ":
Return $ this-> file_array ['size'];
Break;
Default:
Die ("invalid operation! ");
Break;
}
}
}

Http://www.bkjia.com/PHPjc/631718.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/631718.htmlTechArticleclass upload {/*** name attribute of the html form input field, default: 'file' */var $ file_field = 'file '; /*** $ _ files array */var $ file_array;/*** save path. the default path is the current path...

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.