Php Common File Upload class-PHP source code

Source: Internet
Author: User
Ec (2); php Common File Upload Class & lt ;? Php ** & nbsp; * flieclass & nbsp; * (jpg, gif, png) & nbsp; * classUpload {& nbsp; var $ _ file; & nbsp; var $ _ fileType; & nbsp; var $ targetupload; & nbsp; ** & nbsp; * co script ec (2); script

Php Common File Upload class

/**
* Flie class
* (Jpg, gif, png)
*/
Class Upload {
Var $ _ file;
Var $ _ fileType;
Var $ target = 'upload /';

/**
* Construct for this class
*
* @ Param string $ name
* @ Return Upload
*/
Function Upload ($ name ){
If (isset ($ _ FILES [$ name]) {
$ This-> _ file = & $ _ FILES [$ name];
$ This-> _ parseUploadFile ();
} Else {
Die ('no file upload .');
}
}

/**
* Set upload target path
*
* @ Param string $ path
* @ Return boolean
*/
Function setTarget ($ path = 'upload /'){
If (is_dir ($ path )){
$ This-> target = rtrim ($ path ,'/').'/';
Return true;
} Else {
Return false;
}
}

/**
* Get the type of the file
*
*/
Function _ parseUploadFile (){
$ Type = $ this-> _ file ['type'];
If (isset ($ type) & $ type! = ''){
Switch ($ type ){
Case 'image/gif ':
$ This-> _ fileType = 'gif ';
Break;
Case 'image/png ':
$ This-> _ fileType = 'png ';
Break;
Case 'image/jpeg ':
$ This-> _ fileType = 'jpg ';
Break;
Case 'image/pjpeg ':
$ This-> _ fileType = 'jpg ';
Break;
Default:
$ This-> _ fileType = 'unknow ';
Break;
}
} Else {
$ Filename = $ this-> _ file ['name'];
$ Filename = explode ('.', $ filename );
$ Filename = strtoupper ($ filename [sizeof ($ filename)-1]);
Switch ($ filename ){
Case 'png ':
$ This-> _ fileType = 'png ';
Break;
Case 'jpeg ':
$ This-> _ fileType = 'jpg ';
Break;
Case 'jpg ':
$ This-> _ fileType = 'jpg ';
Break;
Case 'gif ':
$ This-> _ fileType = 'gif ';
Break;
Default:
$ This-> _ fileType = 'unknow ';
Break;
}
Unset ($ filename );
}
Unset ($ type );
}

/**
* Upload file
*
* @ Return array
*/
Function load (){
If ($ this-> _ fileType = 'unknow '){
Die ('can not upload this file, because the type is not allow .');
}
If (file_exists ($ this-> _ file ['tmp _ name']) & is_uploaded_file ($ this-> _ file ['tmp _ name']) {
$ New_file_name = $ this-> target. time (). '.'. $ this-> _ fileType;
Move_uploaded_file ($ this-> _ file ['tmp _ name'], $ new_file_name );

Return array ('name' => $ new_file_name, 'SIZE' => $ this-> _ file ['SIZE'], 'type' => $ this-> _ fileType );
} Else {
Return false;
}
}
}
?>

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.