Php file upload code

Source: Internet
Author: User
Tags imagejpeg
Php file Upload class code. For more information about php learning, see. This code is well encapsulated for upload processing. for details, refer. The code is as follows:


/**
* File Upload class
*/
Class uploadFile {
Public $ max_size = '000000'; // sets the size of the uploaded file.
Public $ file_name = 'date'; // The Rename method indicates that the name is named by Time. Otherwise, the name is used.
Public $ allow_types; // the file extension that can be uploaded. different file types are separated by "| ".
Public $ errmsg = ''; // error message
Public $ uploaded = ''; // name of the uploaded file (including the file path)
Public $ save_path; // path for saving the uploaded file
Private $ files; // waiting for file submission
Private $ file_type = array (); // file type
Private $ ext = ''; // upload file extension
/**
* Constructor, initialization class
* @ Access public
* @ Param string $ file_name name of the uploaded file
* @ Param string $ save_path the destination folder to be uploaded
*/
Public function _ construct ($ save_path = './upload/', $ file_name = 'Date', $ allow_types = ''){
$ This-> file_name = $ file_name; // The Rename method indicates that the object is named by Time, and the others are named by the given name.
$ This-> save_path = (preg_match ('/\/$/', $ save_path ))? $ Save_path: $ save_path .'/';
$ This-> allow_types = $ allow_types = ''? 'Jpg | gif | png | zip | rar ': $ allow_types;
}
/**
* Upload a file
* @ Access public
* @ Param $ files the file to be uploaded ($ _ FILES [] from the form)
* @ Return boolean returns a boolean value.
*/
Public function upload_file ($ files ){
$ Name = $ files ['name'];
$ Type = $ files ['type'];
$ Size = $ files ['size'];
$ Tmp_name = $ files ['tmp _ name'];
$ Error = $ files ['error'];
Switch ($ error ){
Case 0: $ this-> errmsg = '';
Break;
Case 1: $ this-> errmsg = 'exceeds the file size in php. ini ';
Break;
Case 2: $ this-> errmsg = 'exceeds the file size specified by the MAX_FILE_SIZE option ';
Break;
Case 3: $ this-> errmsg = 'only part of the file is upload ';
Break;
Case 4: $ this-> errmsg = 'No file is upload ';
Break;
Case 5: $ this-> errmsg = 'upload file size is 0 ';
Break;
Default: $ this-> errmsg = 'File Upload failed! ';
Break;
}
If ($ error = 0 & is_uploaded_file ($ tmp_name )){
// Check the file type
If ($ this-> check_file_type ($ name) = FALSE ){
Return FALSE;
}
// Check the file size
If ($ size> $ this-> max_size ){
$ This-> errmsg = 'upload file '. $ name.' is too large and supports '. ceil ($ this-> max_size/1024). 'kb File' at the maximum ';
Return FALSE;
}
$ This-> set_save_path (); // sets the file storage path.
$ New_name = $ this-> file_name! = 'Date '? $ This-> file_name. '.'. $ this-> ext: date ('ymdhis ').'. '. $ this-> ext; // you can specify a new file name.
$ This-> uploaded = $ this-> save_path. $ new_name; // name of the uploaded file
// Move the file
If (move_uploaded_file ($ tmp_name, $ this-> uploaded )){
$ This-> errmsg = 'file'. $ this-> uploaded. 'uploaded successfully! ';
Return TRUE;
} Else {
$ This-> errmsg = 'file'. $ this-> uploaded. 'Upload failed! ';
Return FALSE;
}
}
}
/**
* Check the Upload file type.
* @ Access public
* @ Param string $ filename name of the file to be checked
* @ Return: If the check succeeds, TRUE is returned and FALSE and error messages are returned.
*/
Public function check_file_type ($ filename ){
$ Ext = $ this-> get_file_type ($ filename );
$ This-> ext = $ ext;
$ Allow_types = explode ('|', $ this-> allow_types); // splits the file extensions that can be uploaded into arrays.
// Echo $ ext;
// Check whether the Upload file extension is in the file extension allowed for Upload
If (in_array ($ ext, $ allow_types )){
Return TRUE;
} Else {
$ This-> errmsg = 'upload file '. $ filename. 'type error, only supports upload '. str_replace ('|', $ this-> allow_types ). 'and other file types! ';
Return FALSE;
}
}
/**
* Obtain the file type
* @ Access public
* @ Param string $ filename the target file name of the file type to be obtained.
* @ Return string file type
*/
Public function get_file_type ($ filename ){
$ Info = pathinfo ($ filename );
$ Ext = $ info ['extension'];
Return $ ext;
}
/**
* Set the path for storing uploaded files.
*/
Public function set_save_path (){
$ This-> save_path = (preg_match ('/\/$/', $ this-> save_path ))? $ This-> save_path: $ this-> save_path .'/';
If (! Is_dir ($ this-> save_path )){
// Create a directory if the directory does not exist
$ This-> set_dir ();
}
}
/**
* Create a directory
* @ Access public
* @ Param string $ dir path of the directory to be created
* @ Return boolean return error messages and FALSE
*/
Public function set_dir ($ dir = null ){
// Check whether the path exists
If (! $ Dir ){
$ Dir = $ this-> save_path;
}
If (is_dir ($ dir )){
$ This-> errmsg = 'the folder to be created already exists! ';
}
$ Dir = explode ('/', $ dir );
Foreach ($ dir as $ v ){
If ($ v ){
$ D. = $ v .'/';
If (! Is_dir ($ d )){
$ State = mkdir ($ d, 0777 );
If (! $ State)
$ This-> errmsg = 'Error when creating the directory'. $ d! ';
}
}
}
Return true;
}
}
/*************************************** **********
* Image processing
*
* Thumbnails and watermarks can be generated for images.
* The default encoding of this class is UTF8. if you want to use it in GBK, add the Chinese character string watermark iconv in the img_mark method and remove it.
*
* The size (in pixels) of UTF8 Chinese characters and English letters is difficult to determine. if there are too many Chinese and English characters in combination, the character string may be left-aligned.
* Or to the right. perform the following operations on $ strc_w = strlen ($ this-> mark_str) * 7 + 5 in the get_mark_xy method according to the project environment.
* Line adjustment
* The GD Library is required. we recommend that you use the GD Library 2.0 + for better use.
*
* @ Author kickflip @ php100 QQ263340607
**************************************** *********/
Class uploadImg extends uploadFile {
Public $ mark_str = 'kickflip @ php100'; // watermark string
Public $ str_r = 0; // string color R
Public $ str_g = 0; // string color G
Public $ str_ B = 0; // string color B
Public $ mark_ttf = './upload/SIMSUN. TTC'; // watermark text font file (including path)
Public $ mark_logo = './upload/logo.png'; // watermark image
Public $ resize_h; // The height of the generated thumbnail.
Public $ resize_w; // Generate the thumbnail width
Public $ source_img; // source image file
Public $ dst_path = './upload/'; // Directory for storing Thumbnail files. If this parameter is not specified, the directory for storing source images is used.
/**
* Generate a thumbnail generated image
* @ Access public
* @ Param integer $ w after narrowing down the image width (px)
* @ Param integer $ h after narrowing down the image height (px)
* @ Param string $ source_img source image (path + file name)
*/
Public function img_resized ($ w, $ h, $ source_img = NULL ){
$ Source_img = NULL? $ This-> uploaded: $ source_img; // Obtain the source file address. if it is null, the image uploaded last time is used by default.
If (! Is_file ($ source_img) {// check whether the source image exists
$ This-> errmsg = 'file'. $ source_img. 'does not exist ';
Return FALSE;
}
$ This-> source_img = $ source_img;
$ Img_info = getimagesize ($ source_img );
$ Source = $ this-> img_create ($ source_img); // create a source image
$ This-> resize_w = $ w;
$ This-> resize_h = $ h;
$ Thumb = imagecreatetruecolor ($ w, $ h );
Imagecopyresized ($ thumb, $ source, $ w, $ h, $ img_info [0], $ img_info [1]); // generates a thumbnail.
$ Dst_path = $ this-> dst_path = ''? $ This-> save_path: $ this-> dst_path; // Obtain the path of the target folder.
$ Dst_path = (preg_match ('// $/', $ dst_path ))? $ Dst_path: $ dst_path. '/'; // add/
If (! Is_dir ($ dst_path) $ this-> set_dir ($ dst_path); // if the target folder does not exist, create
$ Dst_name = $ this-> set_newname ($ source_img );
$ This-> img_output ($ thumb, $ dst_name); // output the image
Imagedestroy ($ source );
Imagedestroy ($ thumb );
}
/**
* Watermark
* @ Access public
* @ Param string $ source_img source image path + file name
* @ Param integer $ mark_type watermark type (1 is an English string, 2 is a Chinese string, 3 is an image logo, default is an English string)
* @ Param integer $ mark_postion watermark position (1 is in the lower left corner, 2 is in the lower right corner, 3 is in the upper left corner, 4 is in the upper right corner, the default is in the lower right corner );
* @ Return: watermark image
*/
Public function img_mark ($ source_img = NULL, $ mark_type = 1, $ mark_postion = 2 ){
$ Source_img = NULL? $ This-> uploaded: $ source_img; // Obtain the source file address. if it is null, the image uploaded last time is used by default.
If (! Is_file ($ source_img) {// check whether the source image exists
$ This-> errmsg = 'file'. $ source_img. 'does not exist ';
Return FALSE;
}
$ This-> source_img = $ source_img;
$ Img_info = getimagesize ($ source_img );
$ Source = $ this-> img_create ($ source_img); // create a source image
$ Mark_xy = $ this-> get_mark_xy ($ mark_postion); // Obtain the Watermark Position
$ Mark_color = imagecolorallocate ($ source, $ this-> str_r, $ this-> str_g, $ this-> str_ B );
Switch ($ mark_type ){
Case 1: // add an English string watermark
$ Str = $ this-> mark_str;
Imagestring ($ source, 5, $ mark_xy [0], $ mark_xy [1], $ str, $ mark_color );
$ This-> img_output ($ source, $ source_img );
Break;
Case 2: // add a Chinese string watermark
If (! Is_file ($ this-> mark_ttf) {// check whether the font file exists
$ This-> errmsg = 'watermark failed: font file'. $ this-> mark_ttf. 'does not exist! ';
Return FALSE;
}
$ Str = $ this-> mark_str;
// $ Str = iconv ('gbk', 'utf-8', $ str); // If you use gbk encoding, remove this line of comment.
Imagettftext ($ source, 12, 0, $ mark_xy [2], $ mark_xy [3], $ mark_color, $ this-> mark_ttf, $ str );
$ This-> img_output ($ source, $ source_img );
Break;
Case 3: // add an image watermark
If (is_file ($ this-> mark_logo) {// if there is a watermark logo image, obtain the basic information of the logo image. if there is no logo image, exit.
$ Logo_info = getimagesize ($ this-> mark_logo );
} Else {
$ This-> errmsg = 'watermark failed: logo file'. $ this-> mark_logo. 'does not exist! ';
Return FALSE;
}
$ Logo_info = getimagesize ($ this-> mark_logo );
If ($ logo_info [0]> $ img_info [0] | $ logo_info [1]> $ img_info [1]) {// exit if the source image is smaller than the logo size
$ This-> errmsg = 'watermark failed: source Image '. $ this-> source_img.' is smaller than '. $ this-> mark_logo! ';
Return FALSE;
}
$ Logo = $ this-> img_create ($ this-> mark_logo );
Imagecopy ($ source, $ logo, $ mark_xy [4], $ mark_xy [5], 0, 0, $ logo_info [0], $ logo_info [1]);
$ This-> img_output ($ source, $ source_img );
Break;
Default: // others are text images.
$ Str = $ this-> mark_str;
Imagestring ($ source, 5, $ mark_xy [0], $ mark_xy [1], $ str, $ mark_color );
$ This-> img_output ($ source, $ source_img );
Break;
}
Imagedestroy ($ source );
}
/**
* Watermark Position
* @ Access private
* @ Param integer $ mark_postion position of the watermark (1 is in the lower left corner, 2 is in the lower right corner, 3 is in the upper left corner, 4 is in the upper right corner, and other is in the lower right corner)
* @ Return array $ mark_xy coordinate of the Watermark Position (index 0 is the coordinate X of the English string watermark, index 1 is the coordinate Y of the English string watermark,
* Index 2 is the coordinate X of the Chinese string watermark, index 3 is the coordinate Y of the Chinese string watermark, index 4 is the coordinate X of the watermark image, and index 5 is the coordinate Y of the watermark image)
*/
Private function get_mark_xy ($ mark_postion ){
$ Img_info = getimagesize ($ this-> source_img );
$ Stre_w = strlen ($ this-> mark_str) * 9 + 5; // length of the watermark English string (px) (The English character size of the 5th character is about 9px. add 5px for the sake of appearance)
// (The size of the Chinese character 12 is 12px. in utf8, the length of a Chinese character is 3 bytes, the size of a byte is 4px, and the length of an English character is about 9px.
// In order to display the full length of X 7px in both Chinese and English)
$ Strc_w = strlen ($ this-> mark_str) * 7 + 5; // length of the watermark Chinese string (px)
If (is_file ($ this-> mark_logo) {// if an image with a watermark logo exists, obtain the basic information of the logo image.
$ Logo_info = getimagesize ($ this-> mark_logo );
}
// Because the start position of the string is different in the imagestring and imagettftext functions, the Y position of the English and Chinese strings is also different.
// The imagestring function starts from the upper left corner of the text. the reference imagettftext function is from the lower left corner of the text.
Switch ($ mark_postion ){
Case 1: // lower left corner
$ Mark_xy [0] = 5; // watermark English string coordinate X
$ Mark_xy [1] = $ img_info [1]-20; // coordinate of the watermark string Y
$ Mark_xy [2] = 5; // watermark Chinese string coordinate X
$ Mark_xy [3] = $ img_info [1]-5; // watermark Chinese string coordinate Y
$ Mark_xy [4] = 5; // watermark image coordinate X
$ Mark_xy [5] = $ img_info [1]-$ logo_info [1]-5; // watermark image coordinate Y
Break;
Case 2: // bottom right corner
$ Mark_xy [0] = $ img_info [0]-$ stre_w; // coordinate of the watermark string X
$ Mark_xy [1] = $ img_info [1]-20; // coordinate of the watermark string Y
$ Mark_xy [2] = $ img_info [0]-$ strc_w; // coordinate X of the watermark Chinese string
$ Mark_xy [3] = $ img_info [1]-5; // watermark Chinese string coordinate Y
$ Mark_xy [4] = $ img_info [0]-$ logo_info [0]-5; // watermark image coordinate X
$ Mark_xy [5] = $ img_info [1]-$ logo_info [1]-5; // watermark image coordinate Y
Break;
Case 3: // the upper left corner
$ Mark_xy [0] = 5; // watermark English string coordinate X
$ Mark_xy [1] = 5; // watermark English string coordinate Y
$ Mark_xy [2] = 5; // watermark Chinese string coordinate X
$ Mark_xy [3] = 15; // watermark Chinese string coordinate Y
$ Mark_xy [4] = 5; // watermark image coordinate X
$ Mark_xy [5] = 5; // watermark image coordinate Y
Break;
Case 4: // the upper right corner of the position
$ Mark_xy [0] = $ img_info [0]-$ stre_w; // coordinate of the watermark string X
$ Mark_xy [1] = 5; // watermark English string coordinate Y
$ Mark_xy [2] = $ img_info [0]-$ strc_w; // coordinate X of the watermark Chinese string
$ Mark_xy [3] = 15; // watermark Chinese string coordinate Y
$ Mark_xy [4] = $ img_info [0]-$ logo_info [0]-5; // watermark image coordinate X
$ Mark_xy [5] = 5; // watermark image coordinate Y
Break;
Default: // others are in the lower right corner by default.
$ Mark_xy [0] = $ img_info [0]-$ stre_w; // coordinate of the watermark string X
$ Mark_xy [1] = $ img_info [1]-5; // coordinate of the watermark string Y
$ Mark_xy [2] = $ img_info [0]-$ strc_w; // coordinate X of the watermark Chinese string
$ Mark_xy [3] = $ img_info [1]-15; // watermark Chinese string coordinate Y
$ Mark_xy [4] = $ img_info [0]-$ logo_info [0]-5; // watermark image coordinate X
$ Mark_xy [5] = $ img_info [1]-$ logo_info [1]-5; // watermark image coordinate Y
Break;
}
Return $ mark_xy;
}
/**
* Create a source image
* @ Access private
* @ Param string $ source_img source image (path + file name)
* @ Return img the image created from the target file
*/
Private function img_create ($ source_img ){
$ Info = getimagesize ($ source_img );
Switch ($ info [2]) {
Case 1:
If (! Function_exists ('imagecreatefromgif ')){
$ Source = @ imagecreatefromjpeg ($ source_img );
} Else {
$ Source = @ imagecreatefromgif ($ source_img );
}
Break;
Case 2:
$ Source = @ imagecreatefromjpeg ($ source_img );
Break;
Case 3:
$ Source = @ imagecreatefrompng ($ source_img );
Break;
Case 6:
$ Source = @ imagecreatefromwbmp ($ source_img );
Break;
Default:
$ Source = FALSE;
Break;
}
Return $ source;
}
/**
* Rename an image
* @ Access private
* @ Param string $ source_img source image path + file name
* @ Return string $ dst_name the renamed Image name (path + file name)
*/
Private function set_newname ($ sourse_img ){
$ Info = pathinfo ($ sourse_img );
$ New_name = $ this-> resize_w. '_'. $ this-> resize_h. '_'. $ info ['basename']; // change the file name to wide _ high _ file name.
If ($ this-> dst_path = '') {// if the path to the thumbnail is empty, the default path is the same as the folder in the source file.
$ Dst_name = str_replace ($ info ['basename'], $ new_name, $ sourse_img );
} Else {
$ Dst_name = $ this-> dst_path. $ new_name;
}
Return $ dst_name;
}
/**
* Output image
* @ Access private
* @ Param $ image processed by im
* @ Param $ dst_name name of the output image (path + file name)
* @ Return output image
*/
Public function img_output ($ im, $ dst_name ){
$ Info = getimagesize ($ this-> source_img );
Switch ($ info [2]) {
Case 1:
If (! Function_exists ('imagegif ')){
Imagejpeg ($ im, $ dst_name );
} Else {
Imagegif ($ im, $ dst_name );
}
Break;
Case 2:
Imagejpeg ($ im, $ dst_name );
Break;
Case 3:
Imagepng ($ im, $ dst_name );
Break;
Case 6:
Imagewbmp ($ im, $ dst_name );
Break;
}
}
}
?>


Php file Upload class source code packaging

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.