PHP File Upload class

Source: Internet
Author: User

Introduction: This is a PHP File Upload class details page, introduced and PHP, related knowledge, skills, experience, and some PHP source code.

Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 342709 'rolling = 'no'>

<? PHP/*** File Upload class */class uploadfile {public $ max_size = '000000'; // set the size of the uploaded file to public $ file_name = 'date '; // The rename method indicates that the object is named after the specified time. For others, the name public $ allow_types is used. // the extension of the object to be uploaded, different file types are separated by "|". Public $ errmsg = ''; // error message: Public $ uploaded =''; // uploaded file name (including file path) public $ save_path; // storage path of the uploaded file private $ files; // submit the file to be uploaded. Private $ file_type = array (); // file type private $ ext = ''; // Upload File Extension/*** constructor, initialization class * @ access public * @ P Aram 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 time is named, for others, use 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 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 PHP. INI File Size '; 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 upload'; break; Case 5: $ this-> errmsg = 'size of the uploaded file 0'; break; default: $ this-> errmsg = 'An error occurred while uploading the file! '; 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 <font color = Red> '. $ name. '</font> is too large to support <font color = Red> '. ceil ($ this-& gt; max_size/1024 ). '</font> kb File'; return false;} $ this-> set_save_path (); // set the file storage path $ new_name = $ this-> file_name! = 'Date '? $ This-> file_name. '. '. $ this-> Ext: Date ('ymdhis '). '. '. $ this-> ext; // set the new file name $ this-> uploaded = $ this-> save_path. $ new_name; // the uploaded file name // move the file if (move_uploaded_file ($ tmp_name, $ this-> uploaded )) {$ this-> errmsg = 'file <font color = Red> '. $ this-> uploaded. '</font> upload successful! '; Return true;} else {$ this-> errmsg =' file <font color = Red> '. $ this-> uploaded.' </font> Upload Failed! '; Return false ;}}} /*** check the upload file type ** @ access public * @ Param string $ filename: the file name to be checked * @ return. If the result is true, 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 ); // split the file extensions that can be uploaded into arrays // echo $ ext; // check whether the file extension is in the file extension that can be uploaded if (in_array ($ Ext, $ allow_types) {return true;} else {$ this-> errmsg = 'Upload file <font color = Red> '. $ filename. '</font> type error. Only <font color = Red>' can be uploaded '. str_replace ('|', $ this-> allow_types ). '</font> and other file types! '; Return false ;}} /*** get 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 file storage path after upload */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) {// If the directory does not exist, create the directory $ this-> set_dir ();}} /*** create directory ** @ access public * @ Param string $ dir path of the directory to be created * @ return Boolean Returns 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 <font color = Red> '. $ D! ';}}Return true ;}} /*************************************** * *********** image processing class ** can generate thumbnails for images, watermarking and other operations * the default encoding of this class is utf8. If you want to use it in GBK, add the Chinese character string watermark iconv annotation in the img_mark method. ** the size of utf8 Chinese characters and English letters (pixels) it is hard to determine. If there are too many Chinese and English combinations, the character string may be left or right. Please use the project environment to check $ strc_w = strlen ($ this-> mark_str) in the get_mark_xy method) * 7 + 5 ** line adjustment * requires support from the GD library, we recommend that you use the GD library 2.0 + ** @ author kickflip @ PhP100 qq263340607 ************************* for better use of this category ********************* ****************************/ Class uploadimg extends uploadfile {public $ mark_str = 'kickflip @ php100'; // watermark string public $ str_r = 0; // string color rpublic $ str_g = 0; // string color gpublic $ str_ B = 0; // string color bpublic $ mark_ttf = '. /upload/simsun. TTC '; // watermark text font file (including path) Public $ mark_logo = '. /upload/logo.png '; // watermark image public $ resize_h; // generate a thumbnail height public $ resize_w; // generate a thumbnail width public $ source_img; // source image file public $ dst_path = '. /upload/'; // directory for storing the thumbnail file. If this parameter is not specified, the directory for storing the source image /* ** Generate a thumbnail generated graph * @ access public * @ Param integer $ W zoom out the image width (PX) * @ Param integer $ H zoom out and then 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; // get the source file address. If it is null, the default value is the image uploaded last time if (! Is_file ($ source_img) {// check whether the source image exists $ this-> errmsg = 'file '. $ source_img. 'nonexistent '; return false;} $ this-> source_img = $ source_img; $ img_info = getimagesize ($ source_img); $ source = $ this-> img_create ($ source_img ); // create source image $ this-> resize_w = $ W; $ this-> resize_h = $ h; $ thumb = imagecreatetruecolor ($ W, $ H); imagecopyresized ($ thumb, $ source, 0, 0, 0, $ W, $ H, $ img_info [0], $ img_info [1]); // generate a thumbnail image $ dst_path = $ this-> dst_path =''? $ This-> save_path: $ this-> dst_path; // get the target folder path $ 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 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 the image logo, the default is an English string) * @ Param integer $ mark_postion watermark position (1 is the lower left corner, 2 is the lower right corner, 3 is the upper left corner, 4 is the upper right corner, the default is 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; // get the source file address. If it is null, the default value is the image uploaded last time if (! Is_file ($ source_img) {// check whether the source image exists $ this-> errmsg = 'file '. $ source_img. 'nonexistent '; 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_st R; 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 GBK encoding is used, remove this line of annotation 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 an image with a watermark logo exists, obtain the basic information of the logo image, exit $ logo_info = getimagesize ($ this-> mark_logo);} else {$ this-> errmsg = 'watermark failed: logo file '. $ this-> Ma Rk_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]) {// if the source image is smaller than the logo size, exit $ this-> errmsg = 'watermark failed: Source image '. $ this-> source_img. 'bi '. $ this-> mark_logo. 'Small! '; 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);}/*** obtain the 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, and * 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 plus 5px for the sake of beauty) // (the Chinese character size of the 12px character is 12px, and the length of a Chinese character in utf8 is 3 bytes One byte is 4px while one English character is one byte. The size of one byte is approximately 9px. // to display the length of a fully set to the number of bytes * 7px in both Chinese and English) $ strc_w = strlen ($ this-> mark_str) * 7 + 5; // The length of the watermark Chinese string (PX) if (is_file ($ this-> mark_logo )) {// If a watermark logo image exists, obtain the basic information of the logo image $ logo_info = getimagesize ($ this-> mark_logo );} // because the start position of the string is different between the imagestring function and the imagettftext function, the y position of the English and Chinese strings is different. // The imagestring function starts from the upper left corner of the text. in the lower left corner of the text, refer to switch ($ mark_postion) {Case 1: // $ mark_xy [0] = 5 in the lower left corner of the position; // watermark English character String coordinate x $ mark_xy [1] = $ img_info [1]-20; // watermark string coordinate 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 ybreak; Case 2: // $ mark_xy [0] = $ img_info [0]-$ stre_w in the lower right corner of the position; // the coordinate of the watermark's English string x $ mark_xy [1] = $ img_info [1]-20; // coordinate Y $ mark_xy [2] = $ img_info [0]-$ strc_w; // watermark Chinese string coordinate x $ mark_xy [3] = $ img_info [1]-5; // watermark Chinese string coordinate Y $ mark_x Y [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 ybreak; Case 3: // position $ mark_xy [0] = 5 in the upper left corner; // 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 ybreak; Case 4: // In the upper-right corner of the position, $ mark_xy [0] = $ img_info [0]-$ stre_w; // the coordinate of the watermark string x $ mark_xy [1] = 5; // watermark English string coordinate Y $ mark_xy [2] = $ img_ I Nfo [0]-$ strc_w; // watermark Chinese string coordinate x $ 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 ybreak; default: // others are in the lower right corner by default. $ mark_xy [0] = $ img_info [0]-$ stre_w; // coordinate of the watermark English string x $ mark_xy [1] = $ img_info [1]-5; // coordinate Y $ mark_xy [2] = $ img_info [0]-$ strc_w; // watermark Chinese string coordinate x $ 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 ybreak;} return $ mark_xy ;} /*** create a source image * @ access private * @ Param string $ source_img source image (path + file name) * @ return IMG image created from the target file */private function img_create ($ source_img) {$ info = getimagesize ($ source_img); Switch ($ info [2]) {Case 1: If (! Function_exists ('authorization') {$ source = @ imagecreatefromjpeg ($ source_img);} else {$ source = @ imagecreatefromgif ($ source_img);} break; Case 2: $ source = @ imagecreatefromjpeg ($ source_img); break; Case 3: $ source = @ parse ($ source_img); break; Case 6: $ source = @ imagecreatefromwbmp ($ source_img ); break; default: $ source = false; break;} return $ source;}/*** rename the image * @ access privat E * @ Param string $ source_img source image path + file name * @ return string $ dst_name name of the renamed image (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 _ height _ file name if ($ this-> dst_path = '') {// if the path for storing the thumbnail is empty, the default value is the source file in the same folder $ dst_name = str_replace ($ info ['basename '], $ new_name, $ sourse_img );} else {$ dst_name = $ this-> dst_pa Th. $ new_name;} return $ dst_name ;} /*** output image ** @ access private * @ Param $ image after im Processing * @ 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 ;}}?>

  

Love J2EE follow Java Michael Jackson video station JSON online tools

Http://biancheng.dnbcw.info/php/342709.html pageno: 6.

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.