Powerful php file upload class,

Source: Internet
Author: User

Powerful php file upload class,

The PHP File Upload class is provided in this article for your reference. The functions are as follows:

<? PHP/** File Upload class **/class upfile {private $ file_size; // the size of the uploaded source file is private $ file_tem; // The temporary storage name of the uploaded file is private $ file_name; // Upload File Name private $ file_type; // upload file type private $ file_max_size = 2000000; // maximum private $ file_folder = "uploadFiles" allowed for file uploads "; // File Upload path private $ over_write = false; // whether to overwrite files of the same name // The type of images that can be uploaded is private $ allow_type = array ('image/jpg ', 'image/jpeg ', 'image/png', 'image/pjpeg ', 'image/gif', 'image/bmp ', 'image/x-png '); // constructor class, file: Upload file domain f Unction _ construct ($ file) {$ this-> 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_nam E (); $ 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]) {c Ase 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 * ($ src _ 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 = im Agecreate ($ 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 be helpful for your learning and support for helping customers.

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.