PHP File Upload class program code _php Tutorial

Source: Internet
Author: User
We now just search file upload class has a lot of, but really good upload class is not many, the following I introduce this file upload class is my own use for a long time, very nice code, we can refer to a reference.
The code is as follows Copy Code

/**
* File Upload Class
*/
Class UploadFile {

Public $max _size = ' 1000000 ';//set Upload file size
Public $file _name = ' date ';//renaming means naming by time, others using given name
Public $allow _types;//allow uploading of file extensions, different file types with "|" Separated
Public $errmsg = ";//Error message
Public $uploaded = ';//file name after upload (including file path)
Public $save _path;//upload file save path
private $files;//submitted waiting to upload files
Private $file _type = Array ();//File type
Private $ext = ';//upload file name extension

/**
* Constructors, initializing classes
* @access Public
* @param string $file _name file name after uploading
* @param string $save _path uploaded target folder
*/
Public function __construct ($save _path = './upload/', $file _name = ' Date ', $allow _types = ') {
$this->file_name = $file _name;//renaming means naming by time, others using given names
$this->save_path = (preg_match ('//$/', $save _path))? $save _path: $save _path. '/';
$this->allow_types = $allow _types = = "? ' Jpg|gif|png|zip|rar ': $allow _types;
}

/**
* Upload Files
* @access Public
* @param $files files waiting to be uploaded ($_files[from 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 = ' exceeds the file size 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 files were 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 '. $name. ' Too big, maximum support '. Ceil ($this->max_size/1024). ' KB of files ';
return FALSE;
}
$this->set_save_path ();//Set File storage 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;//The file name after uploading
Moving files
if (Move_uploaded_file ($tmp _name, $this->uploaded)) {
$this->errmsg = ' file '. $this->uploaded. ' Upload successful! ';
return TRUE;
}else{
$this->errmsg = ' file '. $this->uploaded. ' Upload failed! ';
return FALSE;
}

}
}

/**
* Check upload file type
* @access Public
* @param string $filename file name waiting to be checked
* @return return FALSE and error message if checked by return true failed
*/
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 extensions to be an array
Echo $ext;
Check to see if the upload file extension is in the file extension that allows uploading
if (In_array ($ext, $allow _types)) {
return TRUE;
}else{
$this->errmsg = ' upload file '. $filename. ' Type error, only supports upload '. Str_replace (' | ', ', ', $this->allow_types). ' and other file Types! ';
return FALSE;
}
}

/**
* Get file type
* @access Public
* @param string $filename destination 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)) {
If the directory does not exist, create a directory
$this->set_dir ();
}
}


/**
* Create a directory
* @access Public
* @param string $dir The path of the directory to be created
* Return error message and False @return Boolean failure
*/
Public Function set_dir ($dir = null) {
Check if path exists
if (! $dir) {
$dir = $this->save_path;
}
if (Is_dir ($dir)) {
$this->errmsg = ' The folder you need 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 Create directory '. $d. ' Error! ';
}
}
}
return true;
}
}

/*************************************************
* Image Processing Class
*
* Images can be generated thumbnails, watermark and other operations
* The default encoding for this class is UTF8 if you want to use it under GBK, please remove the Chinese string watermark iconv annotation in the Img_mark method
*
* Due to the UTF8 characters and the English letter size (pixels) is not OK, in the Chinese and English mixed appear too long may appear the string partial left
* or partial right, according to the project environment, $strc_w = strlen ($this->mark_str) in the Get_mark_xy method *7+5 into
* Line Adjustment
* GD library support is required for better use of this class recommended using 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 (contains 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 directory

/**
* Generate thumbnail image after generation
* @access Public
* @param integer $w The width of the image (PX)
* @param integer $h The height of the image (PX)
* @param string $source _img source Picture (path + file name)
*/
Public Function img_resized ($w, $h, $source _img = NULL) {
$source _img = $source _img = = NULL? $this->uploaded: $source _img;//Get the address of the source file, and if empty, the last uploaded picture is the default
if (!is_file ($source _img)) {//check if the source picture exists
$this->errmsg = ' file '. $source _img. ' Not present ';
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 thumbnail images
$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); If the destination folder does not exist, create
$DST _name = $this->set_newname ($source _img);
$this->img_output ($thumb, $dst _name);//Output picture
Imagedestroy ($source);
Imagedestroy ($THUMB);
}

/**
* Water Stamp
* @access Public
* @param string $source _img Source picture path + file name
* @param integer $mark _type watermark type (1 is English string, 2 is Chinese string, 3 is image logo, default is English string)
* @param integer $mark _postion watermark Position (1 is the lower left corner, 2 is the lower right corner, 3 is the upper left corner, 4 is the upper right corner, the default is the lower right corner);
* @return Watermark Picture
*/
Public Function Img_mark ($source _img = NULL, $mark _type = 1, $mark _postion = 2) {
$source _img = $source _img = = NULL? $this->uploaded: $source _img;//Get the address of the source file, and if empty, the last uploaded picture is the default
if (!is_file ($source _img)) {//check if the source picture exists
$this->errmsg = ' file '. $source _img. ' Not present ';
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 if the font file exists
$this->errmsg = ' water stamp failed: Font file '. $this->mark_ttf. ' does not exist! ';
return FALSE;
}
$str = $this->mark_str;
$str = Iconv (' GBK ', ' utf-8 ', $str);//Convert character encoding if you use GBK encoding, remove this line comment
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 Picture watermark
if (Is_file ($this->mark_logo)) {//If there is a watermark logo image to obtain the basic information of the logo image, does not exist then exit
$logo _info = getimagesize ($this->mark_logo);
}else{
$this->errmsg = ' Water stamp failed: Logo file '. $this->mark_logo. ' does not exist! ';
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 image is smaller than the logo size
$this->errmsg = ' Water stamp 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://Other is a text picture
$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 the position of the integer $mark _postion watermark (1 is the lower left corner, 2 is the lower right corner, 3 is the upper left corner, 4 is the upper right corner, the other is the lower right corner)
* @return Array $mark The coordinates of the _xy watermark position (index 0 is the English string watermark coordinates x, index 1 is the English string watermark coordinate y,
* Index 2 is the Chinese string watermark coordinates x, index 3 is the Chinese string watermark coordinates y, index 4 is the watermark picture coordinates x, index 5 is the 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; The length of the watermark in English (px) (the English character size of 5th is about 9px for aesthetics and 5px)
(the Chinese character size of the 12th character is 12px, in utf8 a character length is 3 bytes, one byte is 4px, and an English character length is approximately 9px in size of one byte.
In order to display in the case of mixed in Chinese and English, it is fully set to the length of the byte number *7px)
$STRC _w = strlen ($this->mark_str) *7+5; The length of the watermark Chinese string (px)

if (Is_file ($this->mark_logo)) {//If there is a watermark logo image then get the basic information of logo image
$logo _info = getimagesize ($this->mark_logo);
}

Because the imagestring function and the Imagettftext function have different starting positions for the string, the Y position of the English and Chinese strings differs
The imagestring function is referenced from the upper-left corner of the text as the reference Imagettftext function is from the lower-left corner of the text
Switch ($mark _postion) {

Case 1://Position lower left corner
$mark _xy[0] = 5; Watermark English String coordinate x
$mark _xy[1] = $img _info[1]-20;//watermark English string coordinate y
$mark _xy[2] = 5; Watermark Chinese String coordinate 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 lower right corner
$mark _xy[0] = $img _info[0]-$stre _w; Watermark English String coordinate x
$mark _xy[1] = $img _info[1]-20;//watermark English string coordinate y
$mark _xy[2] = $img _info[0]-$strc _w; Watermark Chinese String coordinate 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 upper left corner
$mark _xy[0] = 5; Watermark English String coordinate x
$mark _xy[1] = 5;//watermark English string coordinate y
$mark _xy[2] = 5; Watermark Chinese String coordinate 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 upper right corner
$mark _xy[0] = $img _info[0]-$stre _w; Watermark English String coordinate x
$mark _xy[1] = 5;//watermark English string coordinate y
$mark _xy[2] = $img _info[0]-$strc _w; Watermark Chinese String coordinate 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 coordinate x
$mark _xy[1] = $img _info[1]-5;//watermark English string coordinate y
$mark _xy[2] = $img _info[0]-$strc _w; Watermark Chinese String coordinate 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 image
* @access Private
* @param string $source _img source Picture (path + file name)
* @return img New image from the 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 + file name
* @return string $dst _name renamed picture name (path + file name)
*/
Private Function Set_newname ($sourse _img) {
$info = PathInfo ($sourse _img);
$new _name = $this->resize_w. ' _ '. $this->resize_h. ' _ '. $info [' basename '];//modify the file name to: Width _ 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 Image
* @access Private
* @param pictures after $im processing
* @param the image name (path + file name) after the output of the $DST _name
* @return Output image
*/
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
}
}

}
?>

How to use

The code is as follows Copy Code

Uploading files

PHP Processing files

Call the class file above, and then do the following

The code is as follows Copy Code

$action = addslashes ($_get[' action ');
if ($action = = "Udd")
{
$state =uploadfile (' UploadFile ');
echo $state [' Err '];exit;
if ($state [' err ']) {
Die (');
}
Else
{
echo ' file uploaded! Delete retransmission ';

}


}
else{
Echo ';
Echo ';
Echo ';
Echo ' ;
Echo 'Uploading files';
echo " ";
Echo ';
echo "";
echo "





"; echo ""; echo "echo" "; Echo ';}

http://www.bkjia.com/PHPjc/444610.html www.bkjia.com true http://www.bkjia.com/PHPjc/444610.html techarticle We now just search file upload class has a lot of, but really good upload class is not many, the following I introduce this file upload class is my own use for a long time, very nice code ...

  • 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.