This article describes in detail the powerful php file Upload class, which has some reference value. interested friends can refer to the examples in this article to share with you the php file Upload class, powerful functions for your reference. the specific content is as follows:
File_name = $ _ FILES [$ file] ['name']; // original client file name $ this-> file_type = $ _ FILES [$ file] ['type']; // file Type $ this-> file_tem =$ _ FILES [$ file] ['tmp _ name']; // Save the temporary file name, generally, the system defaults to $ this-> file_size =$ _ FILES [$ file] ['size']; // File Size} // if the folder does not exist, create the folder function creatFolder ($ f_path) {if (! File_exists ($ f_path) {mkdir ($ f_path, 0777) ;}// checks whether the file has exceeded the upload limit function is_big () {if ($ this-> file_size> $ this-> file_max_size) {echo "the file is too large to exceed the limit! "; Exit ;}// check the file type function check_type () {if (! In_array ($ this-> file_type, $ this-> allow_type) {echo "incorrect file Upload type"; exit ;}// check whether the file exists function check_file_name () {if (! File_exists ($ this-> file_tem) {echo "the uploaded file does not exist"; exit ;}// check whether a file of the same name exists and whether the function check_same_file ($ filename) is overwritten) {if (file_exists ($ filename) & $ this-> over_write! = True) {echo "the file with the same name already exists! "; Exit ;}// move the file function move_file ($ filename, $ destination) {if (! Move_uploaded_file ($ filename, $ destination) {echo "mobile file error"; exit ;}// check whether the file is a function is_upload_file () {if (! Is_uploaded_file ($ this-> file_tem) {echo "the file does not exist"; exit ;}// obtain the file suffix function getext () {$ ext = $ this-> file_name; $ extstr = explode ('. ', $ ext); $ count = count ($ extstr)-1; return $ extstr [$ count];} // create a file name function set_name () {return time (). ". ". $ this-> getext ();} // create the function creat_mulu () {$ this-> creatFolder (".. /.. /upload /". date (Ymd); return "upload /". date (Ymd);} // generated file name function files_name () {$ name = $ this-> set _ Name (); $ folder = $ this-> creat_mulu (); return ".. /.. /". $ folder. "/". $ name;} // upload the file function upload_file () {$ f_name = $ this-> files_name (); move_uploaded_file ($ this-> file_tem, $ f_name ); return $ f_name;} // Generate a thumbnail // maximum width: 120, height: 120 function create_simg ($ img_w, $ img_h) {$ name = $ this-> set_name (); $ folder = $ this-> creat_mulu (); $ new_name = ".. /.. /". $ folder. "/s _". $ name; $ imgsize = getimagesize ($ this-> files_name (); switch ($ imgsize [2]) {case 1: if (! Function_exists ("imagecreatefromgif") {echo "your GD library cannot use images in GIF format. use Jpeg or PNG format! Return "; exit () ;}$ im = imagecreatefromgif ($ this-> files_name (); break; case 2: if (! Function_exists ("imagecreatefromjpeg") {echo "your GD library cannot use jpeg images. please use images in other formats! Return "; exit () ;}$ im = imagecreatefromjpeg ($ this-> files_name (); break; case 3: $ im = imagecreatefrompng ($ this-> files_name (); break; case 4: $ im = imagecreatefromwbmp ($ this-> files_name (); break; default: die ("is not filetype right"); exit;} $ src_w = imagesx ($ im); // Obtain the image width $ src_h = imagesy ($ im ); // Obtain the image height $ new_wh = ($ img_w/$ img_h); // ratio of the width to height of the new image $ src_wh = ($ src_w/$ src_h ); // ratio of original image width to height if ($ new_wh <= $ src_wh) {$ f_w = $ img_w; $ f_h = $ f_w * ($ sr C_h/$ src_w);} else {$ f_h = $ img_h; $ f_w = $ f_h * ($ src_w/$ src_h );} if ($ src_w> $ img_w | $ src_h> $ img_h) {if (function_exists ("imagecreatetruecolor ")) {// check whether the function has defined $ new_img = imagecreatetruecolor ($ f_w, $ f_h); if ($ new_img) {imagecopyresampled ($ new_img, $ im, $ f_w, $ f_h, $ src_w, $ src_h); // Copy part of the image and resize it.} else {$ new_img = imagecreate ($ f_w, $ f_h ); imagecopyresized ($ new_img, $ im, $ f_w, $ f_h, $ src_w, $ src_h);} else {$ new_img = I Magecreate ($ f_w, $ f_h); imagecopyresized ($ new_img, $ im, 0, 0, 0, $ f_w, $ f_h, $ src_w, $ src_h );} if (function_exists ('imagejpeg ') {imagejpeg ($ new_img, $ new_name);} else {imagepng ($ new_img, $ new_name);} imagedestroy ($ new_img );} // imagedestroy ($ new_img); return $ new_name ;}}?>
The above is all the content of this article. I hope it will help you learn and support PHP.
For more powerful PHP file Upload related articles, please follow the PHP Chinese network!