PHP File Upload class code _php tips

Source: Internet
Author: User
Tags explode imagejpeg php file upload
Copy Code code as follows:

<?php
/**
* File Upload Class
*/
Class UploadFile {
Public $max _size = ' 1000000 ';//set Upload file size
Public $file _name = ' date ';//The renaming method is named in time, others with the given name
Public $allow _types;//allow uploading of file extensions, different file types with "|" Separated
Public $errmsg = ';//error message
Public $uploaded = ';//uploaded file name (including file path)
Public $save _path;//upload file save path
Private $files//submitted pending upload file
Private $file _type = Array ();//File type
Private $ext = ';//upload file name extension
/**
* Constructors, initializing classes
* @access Public
* @param string $file _name file name after upload
* @param string $save The destination folder that _path uploaded
*/
Public function __construct ($save _path = './upload/', $file _name = ' Date ', $allow _types = ') {
$this->file_name = $file _name;//renaming is named in time, others with 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 files 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 = ' Over the size of file in php.ini ';
Break
Case 2: $this->errmsg = ' exceeds the file size specified by the max_file_size option ';
Break
Case 3: $this->errmsg = ' file is only partially uploaded ';
Break
Case 4: $this->errmsg = ' no file uploaded ';
Break
Case 5: $this->errmsg = ' upload file size is 0 ';
Break
Default: $this->errmsg = ' upload file failed! ';
Break
}
if ($error = = 0 && is_uploaded_file ($tmp _name)) {
Detecting file types
if ($this->check_file_type ($name) = = FALSE) {
return FALSE;
}
Detecting 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->max_size/1024). ' &LT;/FONT&GT;KB's Documents ';
return FALSE;
}
$this->set_save_path ()//setting File Store path
$new _name = $this->file_name!= ' date '? $this->file_name. $this->ext:date (' Ymdhis '). $this->ext;//to set a new file name
$this->uploaded = $this->save_path. $new _name;//file name after uploading
Moving files
if (Move_uploaded_file ($tmp _name, $this->uploaded)) {
$this->errmsg = ' file <font color=red> '. $this->uploaded. ' </font> Upload Success! ';
return TRUE;
}else{
$this->errmsg = ' file <font color=red> '. $this->uploaded. ' </font> upload failed! ';
return FALSE;
}
}
}
/**
* Check upload file type
* @access Public
* @param string $filename file name waiting to be checked
* @return returns FALSE and error messages if the check fails by returning true
*/
Public Function Check_file_type ($filename) {
$ext = $this->get_file_type ($filename);
$this->ext = $ext;
$allow _types = explode (' | ', $this->allow_types);//split allow uploaded file name extension array
Echo $ext;
Check that the upload file extension is in the file name extension that you want to allow to upload
if (In_array ($ext, $allow _types)) {
return TRUE;
}else{
$this->errmsg = ' upload file <font color=red> '. $filename. ' </font> type error, only supports uploading <font color=red> '. Str_replace (' | ', ', ', $this->allow_types). ' </font> file Types! ';
return FALSE;
}
}
/**
* Get file type
* @access Public
* @param string $filename The target file name to get the file type
* @return String file type
*/
Public Function Get_file_type ($filename) {
$info = PathInfo ($filename);
$ext = $info [' extension '];
return $ext;
}
/**
* Set the save path after file 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)) {
Create a directory if the directory does not exist
$this->set_dir ();
}
}
/**
* Create a table of contents
* @access Public
* @param string $dir The path to create a directory
* Return error message and False @return Boolean failure
*/
Public Function set_dir ($dir = null) {
Check if the path exists
if (! $dir) {
$dir = $this->save_path;
}
if (Is_dir ($dir)) {
$this->errmsg = ' The folder you want to create 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 = ' in creating directory <font color=red> '. $d. ' Error! ';
}
}
}
return true;
}
}
/*************************************************
* Picture Processing class
*
* Can be used to generate thumbnails of pictures, watermark and other operations
* The default encoding for this class is UTF8 if you want to use the GBK under the Img_mark method, please remove the Chinese string watermark iconv annotation
*
* Because UTF8 Chinese characters and English alphabet size (pixel) is not OK, in both Chinese and English mixed too much may appear string left
* Or on the right, according to the project environment to the Get_mark_xy method $strc_w = strlen ($this->mark_str) *7+5 into
* Line Adjustment
* Need GD library support, for better use of this class recommend the use of GD library 2.0+
*
* @author kickflip@php100 QQ263340607
*************************************************/
Class Uploadimg extends UploadFile {
Public $mark _str = ' kickflip@php100 '; Watermark String
Public $str _r = 0; String Color R
Public $str _g = 0; String Color g
Public $str _b = 0; String Color b
Public $mark _ttf = './upload/simsun. TTC '; Watermark Text font file (including path)
Public $mark _logo = './upload/logo.png '; Watermark Picture
Public $resize _h;//Generate thumbnail height
Public $resize _w;//Generate thumbnail width
Public $source _img;//Source picture file
Public $dst _path = './upload/';//thumbnail file storage directory, not fill in the source Picture storage directory
/**
* Generate thumbnail image after generation
* @access Public
* @param the integer $w the width of the picture (PX)
* @param integer $h The height of the picture (PX)
* @param string $source _img source Picture (path + filename)
*/
Public Function img_resized ($w, $h, $source _img = NULL) {
$source _img = $source _img = = NULL? $this->uploaded: $source _img;//to obtain the address of the source file, if empty, default to the last uploaded picture
if (!is_file ($source _img)) {//check if the source picture exists
$this->errmsg = ' file '. $source _img. ' does not exist ';
return FALSE;
}
$this->source_img = $source _img;
$img _info = getimagesize ($source _img);
$source = $this->img_create ($source _img); Create a source picture
$this->resize_w = $w;
$this->resize_h = $h;
$thumb = Imagecreatetruecolor ($w, $h);
Imagecopyresized ($thumb, $source, 0,0,0,0, $w, $h, $img _info[0], $img _info[1])//Generate thumbnails
$DST _path = $this->dst_path = = "? $this->save_path: $this->dst_path; Get destination Folder path
$DST _path = (preg_match ('/\/$/', $dst _path))? $DST _path: $dst _path. '/';//Add the target folder after
if (!is_dir ($DST _path)) $this->set_dir ($dst _path); Create if no destination folder exists
$DST _name = $this->set_newname ($source _img);
$this->img_output ($thumb, $dst _name);//Output picture
Imagedestroy ($source);
Imagedestroy ($THUMB);
}
/**
* Fetching Water Prints
* @access Public
* @param string $source _img source picture path + filename
* @param integer $mark _type watermark type (1 for English string, 2 for Chinese string, 3 for picture logo, default for English string)
* @param integer $mark _postion watermark position (1 is lower left corner, 2 is lower right corner, 3 is upper left corner, 4 is upper right corner, default is lower right corner);
* @return a watermark image
*/
Public Function Img_mark ($source _img = NULL, $mark _type = 1, $mark _postion = 2) {
$source _img = $source _img = = NULL? $this->uploaded: $source _img;//to obtain the address of the source file, if empty, default to the last uploaded picture
if (!is_file ($source _img)) {//check if the source picture exists
$this->errmsg = ' file '. $source _img. ' does not exist ';
return FALSE;
}
$this->source_img = $source _img;
$img _info = getimagesize ($source _img);
$source = $this->img_create ($source _img); Create a source picture
$mark _xy = $this->get_mark_xy ($mark _postion);//Get watermark Position
$mark _color = imagecolorallocate ($source, $this->str_r, $this->str_g, $this->str_b);
Switch ($mark _type) {
Case 1://Add English string watermark
$str = $this->mark_str;
Imagestring ($source, 5, $mark _xy[0], $mark _xy[1], $str, $mark _color);
$this->img_output ($source, $source _img);
Break
Case 2://Kazang string watermark
if (!is_file ($this->mark_ttf)) {//check whether the font file exists
$this->errmsg = ' Draw-water print failed: Font file '. $this->mark_ttf. ' Not exist! '
return FALSE;
}
$str = $this->mark_str;
$str = Iconv (' GBK ', ' utf-8 ', $str); Convert character encoding if you use GBK encoding, remove the comment for this row
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 image watermark
if (Is_file ($this->mark_logo)) {//If the image with the watermark logo gets the basic information of the logo picture and exits if it does not exist
$logo _info = getimagesize ($this->mark_logo);
}else{
$this->errmsg = ' Water seal failure: Logo file '. $this->mark_logo. ' ';
return FALSE;
}
$logo _info = getimagesize ($this->mark_logo);
if ($logo _info[0]> $img _info[0] | | | $logo _info[1]> $img _info[1]) {//exit if the source picture is less than the logo size
$this->errmsg = ' Draw-Water print failed: Source picture '. $this->source_img. ' $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 pictures
$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);
}
/**
* Get Watermark Location
* @access Private
* @param integer $mark _postion watermark position (1 is lower left corner, 2 is lower right corner, 3 is upper left corner, 4 is upper right corner, other is lower right corner)
* @return Array $mark _xy the coordinates of the Watermark location (index 0 is the English string watermark coordinates x, index 1 is the English string watermark coordinates y,
* Index 2 is Chinese string watermark coordinates x, index 3 is Chinese string watermark coordinates y, index 4 is watermark picture coordinates x, index 5 is watermark picture coordinates y)
*/
Private Function Get_mark_xy ($mark _postion) {
$img _info = getimagesize ($this->source_img);
$stre _w = strlen ($this->mark_str) *9+5; Watermark English string length (px) (5th characters in English character size is about 9px in order to be beautiful plus 5px)
(The size of the 12th character is 12px, the length of a Chinese character in UTF8 is 3 bytes a byte 4px while an English character length is approximately 9px in size)
In order to display a full set of bytes in both Chinese and English, the length is byte number *7px)
$STRC _w = strlen ($this->mark_str) *7+5; Length of Watermark Chinese string (px)
if (Is_file ($this->mark_logo)) {//If the image of the watermark logo gets the basic information of the logo picture
$logo _info = getimagesize ($this->mark_logo);
}
The Y-position of the English and Chinese strings is different because the imagestring function and the Imagettftext function differ in the starting position of the string.
The imagestring function is a reference from the upper-left corner of the text Imagettftext function is from the lower-left corner of the text
Switch ($mark _postion) {
Case 1://position in lower left corner
$mark _xy[0] = 5; Watermark English String coordinates x
$mark _xy[1] = $img _info[1]-20;//watermark English string coordinates y
$mark _xy[2] = 5; Watermark Chinese String coordinates x
$mark _xy[3] = $img _info[1]-5;//watermark Chinese string coordinate y
$mark _xy[4] = 5;//watermark Picture coordinates x
$mark _xy[5] = $img _info[1]-$logo _info[1]-5;//watermark Picture coordinates y
Break
Case 2://position in the lower right corner
$mark _xy[0] = $img _info[0]-$stre _w; Watermark English String coordinates x
$mark _xy[1] = $img _info[1]-20;//watermark English string coordinates y
$mark _xy[2] = $img _info[0]-$strc _w; Watermark Chinese String coordinates x
$mark _xy[3] = $img _info[1]-5;//watermark Chinese string coordinate y
$mark _xy[4] = $img _info[0]-$logo _info[0]-5;//watermark Picture coordinates x
$mark _xy[5] = $img _info[1]-$logo _info[1]-5;//watermark Picture coordinates y
Break
Case 3://position in upper left corner
$mark _xy[0] = 5; Watermark English String coordinates x
$mark _xy[1] = 5;//watermark English string coordinates y
$mark _xy[2] = 5; Watermark Chinese String coordinates x
$mark _xy[3] = 15;//watermark Chinese string coordinate y
$mark _xy[4] = 5;//watermark Picture coordinates x
$mark _xy[5] = 5;//watermark Picture coordinates y
Break
Case 4://position in the upper right corner
$mark _xy[0] = $img _info[0]-$stre _w; Watermark English String coordinates x
$mark _xy[1] = 5;//watermark English string coordinates y
$mark _xy[2] = $img _info[0]-$strc _w; Watermark Chinese String coordinates x
$mark _xy[3] = 15;//watermark Chinese string coordinate y
$mark _xy[4] = $img _info[0]-$logo _info[0]-5;//watermark Picture coordinates x
$mark _xy[5] = 5;//watermark Picture coordinates y
Break
Default://other defaults to lower right corner
$mark _xy[0] = $img _info[0]-$stre _w; Watermark English String coordinates x
$mark _xy[1] = $img _info[1]-5;//watermark English string coordinates y
$mark _xy[2] = $img _info[0]-$strc _w; Watermark Chinese String coordinates x
$mark _xy[3] = $img _info[1]-15;//watermark Chinese string coordinate y
$mark _xy[4] = $img _info[0]-$logo _info[0]-5;//watermark Picture coordinates x
$mark _xy[5] = $img _info[1]-$logo _info[1]-5;//watermark Picture coordinates y
Break
}
return $mark _xy;
}
/**
* Create a source picture
* @access Private
* @param string $source _img source Picture (path + filename)
* @return img New image from Target file
*/
Private Function Img_create ($source _img) {
$info = getimagesize ($source _img);
Switch ($info [2]) {
Case 1:
if (!function_exists (' imagecreatefromgif ')) {
$source = @imagecreatefromjpeg ($source _img);
}else{
$source = @imagecreatefromgif ($source _img);
}
Break
Case 2:
$source = @imagecreatefromjpeg ($source _img);
Break
Case 3:
$source = @imagecreatefrompng ($source _img);
Break
Case 6:
$source = @imagecreatefromwbmp ($source _img);
Break
Default
$source = FALSE;
Break
}
return $source;
}
/**
* Rename Picture
* @access Private
* @param string $source _img source picture path + filename
* @return String $DST _name the name of the picture (path + filename) after renaming
*/
Private Function Set_newname ($sourse _img) {
$info = PathInfo ($sourse _img);
$new _name = $this->resize_w. $this->resize_h. ' _ '. $info [' basename '];//modify file name to: Wide _ high _ file name
if ($this->dst_path = = ") {//If the thumbnail path is empty, the default is the same folder as the source file
$DST _name = str_replace ($info [' basename '], $new _name, $sourse _img);
}else{
$DST _name = $this->dst_path. $new _name;
}
return $DST _name;
}
/**
* Output Picture
* @access Private
* @param $im the image after processing
* @param $dst _name output of the picture name (path + filename)
* @return Output picture
*/
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
}
}
}
?>

PHP File Upload class source packaging

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.