PHP handwritten Image Upload class-PHP source code

Source: Internet
Author: User
Upload images to the Internet as long as you are willing to search more, but there are several webmasters who will just copy them by themselves. Below I will copy a better one for you, I have also tested the Image Upload code. I hope this class will help you. Upload images to the Internet as long as you are willing to search more, but there are several webmasters who will just copy them by themselves. Below I will copy a better one for you, I have also tested the Image Upload code. I hope this class will help you.

Script ec (2); script

A new handwritten PHP Image Upload class. The basic functions are met. You can return the client file name, file size, Temporary Folder path, supported file types, file suffixes, and new paths, new file name, image width, Image Height, custom upload success message, display the upload status .. Whether the information is uploaded successfully or not, and the constructor are enough for beginners to study ..

In use, like other PHP classes, initialize the object first and call the function method on the frontend, the research value of this class mainly lies in the Code standardization and practicality, and there is no large amount of tedious Code. It is a PHP Image Upload class integrated by some simple upload functions, the following is the detailed class code:

The Code is as follows:

Class upload {
Private $ file_name = 'null'; # client file name
Private $ file_size = 0; # File Size
Private $ file_tmpName = "; # Temporary Folder path
Private $ upload_type = array ('jpg ', 'jpeg', 'gif', 'png '); # types of files that can be uploaded
Private $ upload_Ext = 'null'; # file suffix
Private $ upload_dir = 'null'; # New Path
Private $ upload_name = 'null'; # New File Name
Private $ upload_width = 0; # Image Width
Private $ upload_height = 0; # Image Height
Private $ upload_msg = "; # upload a message
Private $ upload_mode; # upload status
/**
* Constructor for automatic upload
* @ Access public
* @ Param string $ control file domain control name
* @ Param string $ filePath: Save path
* @ Param string $ new fileName file name without a suffix
* @ Param array $ fileType specifies the file suffix that can be uploaded.
* @ Param int $ size the size of the file to be uploaded, in bytes
*/
Public function _ construct ($ control = ", $ filePath =", $ fileName = ", $ fileType =", $ size = 0 ){
$ This-> upload_mode = true;
If ($ control = ")
$ This-> error ('the name of the uploaded file domain control cannot be blank ');
Else {
$ This-> file_name = $ _ FILES [$ control] ['name'];
$ This-> file_size = $ _ FILES [$ control] ['SIZE'];
$ This-> file_tmpName = $ _ FILES [$ control] ['tmp _ name'];
If ($ fileType! = ")
$ This-> upload_type = $ fileType;
If ($ filePath = ")
$ This-> upload_dir = './';
Else
$ This-> upload_dir = $ filePath;
If (! Is_uploaded_file ($ this-> file_tmpName ))
$ This-> error ('no file is upload ');
Else {
If ($ size! = 0 & $ this-> file_size> $ size)
$ This-> error ('the uploaded image file cannot be greater than '. $ this-> getSize ($ size ));
If (! $ This-> typeDetect ($ this-> file_name ))
$ This-> error ('upload of this type of files' is not supported ');
Else {
If (! $ This-> fileDetect ($ this-> file_tmpName ))
$ This-> error ('uploaded image file is invalid or damaged ');
}
If ($ fileName = ")
$ This-> upload_name = $ this-> file_name;
Else
$ This-> upload_name = $ fileName. '.'. $ this-> upload_Ext;
}
}
// Start saving the file
If ($ this-> mode)
$ This-> saveFile ();
}
/**
* Get attributes
* @ Access public
* @ Param string attribute name
* @ Return Attribute Value
*/
Public function _ get ($ name ){
Switch ($ name ){
Case 'path ':
Return $ this-> upload_dir;
Break;
Case 'name ':
Return $ this-> upload_name;
Break;
Case 'ext ':
Return $ this-> upload_Ext;
Break;
Case 'SIZE ':
Return $ this-> getSize ($ this-> file_size );
Case 'width ':
Return $ this-> upload_width;
Break;
Case 'height ':
Return $ this-> upload_height;
Break;
Case 'Mode ':
Return $ this-> upload_mode;
Break;
Case 'msg ':
Return $ this-> upload_msg;
Break;
}
}
/**
* Filename extension
* @ Access public
* @ Return bool
*/
Private function typeDetect ($ OldFile ){
$ TempArr = explode (".", $ OldFile );
$ FileExt = array_pop ($ tempArr );
$ FileExt = trim ($ fileExt );
$ FileExt = strtolower ($ fileExt );
If (in_array ($ fileExt, $ this-> upload_type )){
$ This-> upload_Ext = $ fileExt;
Return true;
}
Else
Return false;
}
/**
* Checks whether the uploaded file is in a valid image format.
* @ Access private
* @ Return bool
*/
Private function fileDetect ($ tmpName ){
$ Arr = getimagesize ($ tmpName );
If (! $ Arr ){
Return false;
} Else {
$ This-> upload_width = $ arr [0];
$ This-> upload_height = $ arr [1];
Return true;
}
}
/**
* Save the file
* @ Access private
*/
Private function saveFile (){
If (! Is_dir ($ this-> upload_dir )){
Mkdir ($ this-> upload_dir, 0777 );
Chmod ($ this-> upload_dir, 0777 );
}
$ Save_file = move_uploaded_file ($ this-> file_tmpName, $ this-> upload_dir. $ this-> upload_name );
If (! $ Save_file)
$ This-> error ('file write failed ');
Else
$ This-> upload_msg = 'file uploaded successfully ';
}
/**
* Get the file size
* @ Return string returns B, KB, in MB
*/
Private function getSize ($ tmpSize ){
$ Value = 'B ';
When ($ tmpSize> 1024 ){
$ TmpSize = floor ($ tmpSize/1024 );
$ Value = 'kb ';
}
When ($ tmpSize> 1024 ){
$ TmpSize = round ($ tmpSize/1024,2 );
$ Value = 'mb ';
}
Return $ tmpSize. $ value;
}
/**
* Error message
* @ Access private
* @ Param string $ msg error message
*/
Private function error ($ msg = "){
$ This-> upload_msg. = $ msg;
$ This-> upload_mode = 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.