Php simple upload class sharing, php Upload sharing

Source: Internet
Author: User

Php simple upload class sharing, php Upload sharing

This article provides examples of php Upload classes for your reference. The specific content is as follows:

<? Phpclass UploadFile {var $ inputName; // Control name var $ allowType = array ('image/gif', 'image/jpg ', 'image/jpeg ', 'image/png ', 'image/x-png', 'image/pjpeg '); // upload type var $ allowSize = 2097152; // limit the size of var $ saveDir = UPLOAD; // Save the directory var $ isRename = true; // whether to rename it; default value: true var $ errID = 0; // error code, the default value is 0 var $ errMsg = ""; // error message var $ savePath = ""; // save path function _ construct ($ inputName, $ allowType = "", $ allowSize = "", $ saveD Ir = "", $ isRename = true) {if (empty ($ inputName) {$ this-> chk_err (-1 ); // no input control name} else {$ this-> inputName = $ inputName;} if (! Empty ($ allowType) $ this-> allowType = $ allowType; if (! Empty ($ allowSize) $ this-> allowSize = $ allowSize; if (! Empty ($ saveDir) $ this-> saveDir = $ saveDir; if (! Empty ($ isRename) $ this-> isRename = $ isRename;} function is_uploaded () {if (empty ($ _ FILES [$ this-> inputName] ['name']) {$ this-> chk_err (4 ); // No file is uploaded} else {if (is_uploaded_file ($ _ FILES [$ this-> inputName] ['tmp _ name']) {return true ;} else {$ this-> chk_err (-2); // The File Upload is invalid.} function chk_type () {if (! In_array ($ _ FILES [$ this-> inputName] ['type'], $ this-> allowType) {$ this-> chk_err (-3 ); // the uploaded file type is not allowed} else {return true ;}} function chk_size () {if ($ _ FILES [$ this-> inputName] ['SIZE']> $ this-> allowSize) {$ this-> chk _ (-4 ); // the uploaded file is too large} else {return true;} function move_uploaded () {// move the uploaded file if (! $ This-> is_uploaded () {return false;} if (! $ This-> chk_size () {return false;} if (! $ This-> chk_type () {return false;} // rename if ($ this-> isRename) {$ arrTmp = pathinfo ($ _ FILES [$ this-> inputName] ['name']); $ extension = strtolower ($ arrTmp ['extension']); $ file_newname = date ("YmdHis "). rand (1000,9999 ). "00. ". $ extension; // rename a new file. 00 indicates the uploaded source image.} else {$ file_newname = $ _ FILES [$ this-> inputName] ['name'];} if (! File_exists ($ this-> saveDir) {// determines whether mkdir ($ this-> saveDir, 0777, true) exists in the saved directory ); // create a save directory} // move the file $ result = move_uploaded_file ($ _ FILES [$ this-> inputName] ['tmp _ name'], $ this-> saveDir. "/". $ file_newname); if ($ result) {$ path = $ this-> savePath = $ this-> saveDir. $ file_newname; // The success save path of the file. return $ path ;} else {$ this-> chk_err ($ _ FILES [$ this-> inputName] ['error']) ;}// judge the error message function chk_err ($ errID) {$ this-> errID = $ ErrID; switch ($ this-> errID) {case-4: $ this-> errMsg = "the uploaded file is too large"; break; case-3: $ this-> errMsg = "the uploaded file type is not allowed"; break; case-2: $ this-> errMsg = "the file upload is invalid"; break; case-1: $ this-> errMsg = "no control name passed in"; break; case 1: $ this-> errMsg = 'the uploaded file exceeds php. maximum value set by upload_max_filesize in ini '; break; case 2: $ this-> errMsg = 'the size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form'; break; case 3: $ this-> errMsg = 'only part of the file is upload'; break; c Ase 4: $ this-> errMsg = 'no file upload'; break; default: break;} return false;} function get_errMsg () {echo $ this-> errMsg; // output error message}/** + response * obtain image information * + response * @ static * @ access public + response * @ param string $ image file name + ------------------- ------------------------------------- * @ Return mixed + parameters */function getImageInfo ($ img) {$ imageInfo = getimagesize ($ img); if ($ imageInfo! = False) {$ imageType = strtolower (substr (image_type_to_extension ($ imageInfo [2]), 1); $ imageSize = filesize ($ img ); $ info = array ("width" => $ imageInfo [0], "height" => $ imageInfo [1], "type" => $ imageType, "size" => $ imageSize, "mime" => $ imageInfo ['mime '],); return $ info ;}else {return false ;}} /** + ---------------------------------------------------------- * generate a thumbnail + -------------------------------- -------------------------- * @ Static * @ access public + parameters * @ param string $ image source image * @ param string $ type image format * @ param string $ thumbname thumbnail file name * @ param string $ maxWidth width * @ param string $ maxHeight height * @ param string $ position thumbnail storage directory * @ param boolean $ interlace enable line scanning * @ param boolean $ is_save whether to retain the source image + ------------------------------------- --------------------- * @ Return void + response */function thumb ($ image, $ is_save = true, $ suofang = 0, $ type = '', $ maxWidth = 500, $ maxHeight = 500, $ interlace = true) {// obtain the source image Information $ info = $ this-> getImageInfo ($ image); if ($ info! = False) {$ srcWidth = $ info ['width']; $ srcHeight = $ info ['height']; $ type = empty ($ type )? $ Info ['type']: $ type; $ type = strtolower ($ type); $ interlistener = $ interlistener? 1:0; unset ($ info); if ($ suofang = 1) {$ width = $ srcWidth; $ height = $ srcHeight ;} else {$ scale = min ($ maxWidth/$ srcWidth, $ maxHeight/$ srcHeight); // calculate the zoom ratio if ($ scale> = 1) {// no longer scale out when the source image size exceeds $ width = $ srcWidth; $ height = $ srcHeight;} else {// thumbnail size $ width = (int) ($ srcWidth * $ scale); // 147 $ height = (int) ($ srcHeight * $ scale ); // 199} // load the source image $ createFun = 'imagecreatefrom '. ($ type = 'jpg '? 'Jpeg ': $ type); $ srcImg = $ createFun ($ image); // create a thumbnail if ($ type! = 'Gif' & function_exists ('imagecreatetruecolor') $ thumbImg = imagecreatetruecolor ($ width, $ height); else $ thumbImg = imagecreate ($ width, $ height ); // copy the image if (function_exists ("ImageCopyResampled") imagecopyresampled ($ thumbImg, $ srcImg, 0, 0, 0, 0, $ width, $ height, $ srcWidth, $ srcHeight); else imagecopyresized ($ thumbImg, $ srcImg, 0, 0, 0, 0, $ width, $ height, $ srcWidth, $ srcHeight ); if ('gif' = $ type | | 'Png '= $ type) {// imagealphablending ($ thumbImg, false); // cancel the default color mixing mode // imagesavealpha ($ thumbImg, true ); // set and save the complete alpha channel information $ background_color = imagecolorallocate ($ thumbImg, 0,255, 0); // assign a green imagecolortransparent ($ thumbImg, $ background_color ); // set to transparent color, if this line is commented out, a green image will be output.} // you can set a line scan for the jpeg image if ('jpg '= $ type | 'jpeg' = $ type) imageinterlace ($ thumbImg, $ interlace); // $ gray = ImageColorAllocate ($ thumbImg, 255 ,); // ImageString ($ thumbImg, 5, "ThinkPHP", $ gray); // generate an image $ imageFun = 'image '. ($ type = 'jpg '? 'Jpeg ': $ type); $ length = strlen ("00. ". $ type) * (-1); $ _ type = substr ($ image,-4); $ length = ($ type! =_ _ Type? $ Length + 1: $ length); // crop if ($ suofang = 1) {$ thumbname01 = substr_replace ($ image, "01. ". $ type, $ length); // large Avatar $ thumbname02 = substr_replace ($ image, "02. ". $ type, $ length); // small Avatar $ imageFun ($ thumbImg, $ thumbname01, 100); $ imageFun ($ thumbImg, $ thumbname02, 100 ); $ thumbImg01 = imagecreatetruecolor (190,195); imagecopyresampled ($ thumbImg01, $ thumbImg, 190,195, $ _ POST ['X'], $ _ POST ['y, $ _ POST ['W'], $ _ POST ['H']); $ thumbImg02 = imagecreatetruecolor (48, 48); imagecopyresampled ($ thumbImg02, $ thumbImg, 0, 0, $ _ POST ['X'], $ _ POST ['y'], 48, 48, $ _ POST ['W'], $ _ POST ['H']); $ imageFun ($ thumbImg01, $ thumbname01, 100); $ imageFun ($ thumbImg02, $ thumbname02, 100); // unlink ($ image); imagedestroy ($ thumbImg01 ); imagedestroy ($ thumbImg02); imagedestroy ($ thumbImg); imagedestroy ($ srcImg); return array ('Big '=> $ thumbname01, 'small' => $ thumbname02 ); // returns an array containing the size of the Avatar path} else {if ($ is_save = false) {// The thumbnail overwrites the source image, and the path of the thumbnail is still the source image path $ imageFun ($ thumbImg, $ image, 100);} else {$ thumbname03 = substr_replace ($ image, "03. ". $ type, $ length); // The thumbnail and the source image coexist. $ imageFun ($ thumbImg, $ thumbname03, 100); imagedestroy ($ thumbImg); imagedestroy ($ srcImg ); return $ thumbname03; // return the path of the thumbnail, string }}return false ;}}

The above is all the content of this article. I hope it will help you learn php programming.

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.