PHP File Upload

Source: Internet
Author: User
Tags php file upload transparent image unsupported
PHP File Upload $ _ FILES ['file'] ['error'] Starting from PHP 4.2.0, PHP will return a corresponding error along with the file information array Code . This code can be found in the error field of the file array generated during file upload, that is, $ _ FILES ['file'] ['error']. The value of upload_err_ OK is 0. If no error occurs, the file is uploaded successfully. The value of upload_err_ini_size is 1. the uploaded file exceeds the limit of the upload_max_filesize option in PHP. ini. The value of upload_err_form_size is 2. The size of the uploaded file exceeds the value specified by the max_file_size option in the HTML form. The value of upload_err_partial is 3, and only part of the file is uploaded. The value of upload_err_no_file is 4, and no file is uploaded. The value of upload_err_no_tmp_dir is 6, and the Temporary Folder cannot be found. PHP 4.3.10 and PHP 5.0.3 are introduced. The value of upload_err_cant_write is 7, and file writing fails. PHP 5.1.0 is introduced. Note: The above value is changed to a PHP constant after PHP 4.3.0.

<? PHP
/****************************
Upload class by: donnier
****************************/
Class upload_class {
Private $ ptname; // The Name Of The upload form;
Private $ udname; // whether to create a subdirectory by month (0 indicates no, others are true );
Private $ ufname; // whether to create a file name by time (0 indicates no, others are true );
Private $ ultype; // The type of the uploaded file;
Private $ ulsize; // the size of the uploaded file;
Private $ ulname; // name of the output file;
Private $ ulpath; // output file path;
Private $ WM; // Add a watermark (0 is not added, and others are added );
Private $ wmtype; // watermark type (0 indicates text, and others indicates image );
Private $ wmpic; // watermark image;
Private $ wmpicquality; // image quality;
Private $ wmpictrans; // transparent image;
Private $ wmstr; // watermark character;
Private $ wmstrsize; // character size;
Private $ wmstrfont; // Character Font;
Private $ wmstrcolor; // character color;
Private $ wmpos; // watermark position (1: top left, 2: Top center, 3: Top right ......);
Function _ construct ($ ptname = 'upfile', $ udname = 1, $ ufname = 1, $ ultype = array ('image/jpg ', 'image/JPEG ', 'image/PNG ', 'image/pjpeg', 'image/gif', 'image/BMP ', 'image/X-PNG'), $ WM = 1, $ wmtype = 1, $ wmpic = 'images/wm.gif ', $ ulsize = 2097152, $ ulpath = 'images/temp/', $ wmpictrans = 20, $ wmpicquality = 80, $ wmstr = 'donline', $ wmstrsize = 5, $ wmstrfont = '. /font/cour. ttf', $ wmstrcolor = '# ff0000', $ wmpos = 9 ){
$ This-> ptname = $ _ FILES [$ ptname];
$ This-> udname = $ udname;
$ This-> ufname = $ ufname;
$ This-> ultype = $ ultype;
$ This-> ulsize = $ ulsize;
$ This-> ulpath = $ ulpath;
$ This-> WM = $ WM;
$ This-> wmtype = $ wmtype;
$ This-> wmpic = $ wmpic;
$ This-> wmpicquality = $ wmpicquality;
$ This-> wmpictrans = $ wmpictrans;
$ This-> wmstr = $ wmstr;
$ This-> wmstrsize = $ wmstrsize;
$ This-> wmstrfont = $ wmstrfont;
$ This-> wmstrcolor = $ wmstrcolor;
$ This-> wmpos = $ wmpos;
}
Function uploadfun (){
If ($ _ server ['request _ method'] = 'post '){
If (! Is_uploaded_file ($ this-> ptname ['tmp _ name']) $ this-> errorfun ('upload failed! ');
If (! In_array ($ this-> ptname ['type'], $ this-> ultype) $ this-> errorfun ('unsupported file type! ');
If ($ this-> ulsize <$ this-> ptname ['SIZE']) $ this-> errorfun ('file is too large! ');
If ($ this-> udname) {date_default_timezone_set ('utc'); $ this-> ulpath = $ this-> ulpath. 'month _'. date ('ym '). '/';}
Else {$ this-> ulpath = $ this-> ulpath ;}
$ This-> createfun ($ this-> ulpath );
If ($ this-> ufname) {$ T = pathinfo ($ this-> ptname ['name']); $ this-> ulname = $ this-> ulpath. time (). '. '. $ t ['extension'];}
Else {$ this-> ulname = $ this-> ulpath. $ this-> ptname ['name'];}
If (file_exists ($ this-> ulname) $ this-> errorfun ('this file already exists! ');
If (! Move_uploaded_file ($ this-> ptname ['tmp _ name'], $ this-> ulname) $ this-> errorfun ('file moving error! ');
$ This-> wmfun ();
$ This-> errorfun ('upload successful! ');
}
}
Function createfun ($ d ){
If (! File_exists ($ D) {$ this-> createfun (dirname ($ D); mkdir ($ D );}
}
Function wmfun (){
If ($ this-> WM ){
If (file_exists ($ this-> ulname )){
$ Groundimg = getimagesize ($ this-> ulname );
$ Ow = $ groundimg [0];
$ Oh = $ groundimg [1];
Switch ($ groundimg [2]) {
Case 1: $ G = imagecreatefromgif ($ this-> ulname); break;
Case 2: $ G = imagecreatefromjpeg ($ this-> ulname); break;
Case 3: $ G = imagecreatefrompng ($ this-> ulname); break;
Case 4: $ G = imagecreatefromwbmp ($ this-> ulname); break;
Default: $ this-> errorfun ('unsupported background image type! ');
}
}
Else {$ this-> errorfun ('the background image does not exist! ');}
If (file_exists ($ this-> wmpic )){
$ Wmimg = getimagesize ($ this-> wmpic );
$ WW = $ wmimg [0];
$ Wh = $ wmimg [1];
Switch ($ wmimg [2]) {
Case 1: $ W = imagecreatefromgif ($ this-> wmpic); break;
Case 2: $ W = imagecreatefromjpeg ($ this-> wmpic); break;
Case 3: $ W = imagecreatefrompng ($ this-> wmpic); break;
Case 4: $ W = imagecreatefromwbmp ($ this-> wmpic); break;
Default: $ this-> errorfun ('unsupported watermark image type! ');
}
}
Else {$ this-> errorfun ('the watermark image does not exist! ');}
Switch ($ this-> wmtype ){
Case 0: $ TP = imagettfbbox (Ceil ($ this-> wmstrsize * 2.5), 0, $ this-> wmstrfont, $ this-> wmstr ); $ WW = $ TP [2]-$ TP [6]; $ wh = $ TP [3]-$ TP [7]; unset ($ TP); break;
Case 1: $ WW = $ WW; $ wh = $ Wh; break;
Default: $ WW = $ WW; $ wh = $ Wh; break;
}
If ($ ow <$ WW | $ Oh <$ Wh) $ this-> errorfun ('the background image is too small! The watermark cannot be generated! ');
Switch ($ this-> wmpos ){
Case 0: $ x = rand (0, ($ ow-$ ww); $ Y = rand (0, ($ oh-$ Wh); break; // random
Case 1: $ x = 0; $ y = 0; break; // 1 is the top left
Case 2: $ x = ($ ow-$ ww)/2; $ y = 0; break; // 2 center the top
Case 3: $ x = $ ow-$ WW; $ y = 0; break; // 3: Top right
Case 4: $ x = 0; $ Y = ($ oh-$ Wh)/2; break; // 4 is left in the middle
Case 5: $ x = ($ ow-$ ww)/2; $ Y = ($ oh-$ Wh)/2; break; // 5 is center
Case 6: $ x = $ ow-$ WW; $ Y = ($ oh-$ Wh)/2; break; // 6 is left in the middle
Case 7: $ x = 0; $ Y = $ oh-$ Wh; break; // 7 is left at the bottom
Case 8: $ x = ($ ow-$ ww)/2; $ Y = $ oh-$ Wh; break; // 8 is centered at the bottom
Case 9: $ x = $ ow-$ WW; $ Y = $ oh-$ Wh; break; // 9 is the bottom right
Default: $ x = rand (0, ($ ow-$ ww); $ Y = rand (0, ($ oh-$ Wh); break; // random
}
Imagealphablending ($ g, true );
Switch ($ this-> wmtype ){
Case 0:
If ($ this-> wmstrcolor) {$ r = hexdec (substr ($ this-> wmstrcolor, 1, 2); $ G = hexdec (substr ($ this-> wmstrcolor, 3, 2); $ B = hexdec (substr ($ this-> wmstrcolor, 5 ));}
Else {$ this-> errorfun ('the watermark text color does not exist! ');}
Imagestring ($ g, $ this-> wmstrfont, $ X, $ y, $ this-> wmstr, imagecolorallocate ($ g, $ R, $ g, $ B )); break;
Case 1; imagecopymerge ($ g, $ W, $ X, $ y, 0, $ WW, $ Wh, $ this-> wmpictrans); break;
Default: imagecopymerge ($ g, $ W, $ X, $ y, 0, $ WW, $ Wh, $ this-> wmpictrans); break;
}
@ Unlink ($ this-> ulname );
Switch ($ groundimg [2]) {
Case 1: imagegif ($ g, $ this-> ulname); break;
Case 2: imagejpeg ($ g, $ this-> ulname, $ this-> wmpicquality); break;
Case 3: imagepng ($ g, $ this-> ulname); break;
Case 4: imagewbmp ($ g, $ this-> ulname); break;
Default: $ this-> errorfun ('image generation failed! ');
}
If (isset ($ wmimg) unset ($ wmimg );
If (isset ($ W) imagedestroy ($ W );
Unset ($ groundimg );
Imagedestroy ($ G );
}
}
Function errorfun ($ e = 'unknown error! '){
$ MSG = '<script language = "JavaScript" type = "text/JavaScript"> ';
$ Msg. = 'alert ("'. $ E .'");';
$ Msg. = 'History. Back ();';
$ Msg. = '</SCRIPT> ';
Echo $ MSG;
Exit;
}
}
?>

<Form enctype = "multipart/form-Data" method = "Post" Action = "upfile. php">
<Input name = "upfile" type = "file"/>
<Input type = "Submit" name = "Submit" value = "Submit"/>

PHP File
Require_once ('inc/upload_class.php ');
$ Ul = new upload_class ();
$ Ul-> uploadfun ();

Related Article

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.