PHP very practical upload class, upload effect online demo

Source: Internet
Author: User
Tags date array exit chmod file size flock hash imagejpeg

#*********************************************************

#文件名称:

inc_class.upload.php

#Copyright (c)

2007-2009 Youth once all rights reserved.

#最后更新: 2009-08-05

#版本: v 2.0.a

#注: Please keep this declaration information when forwarding, this statement will not affect your speed!

#如有修改请将修改后的文件以邮件形式发送给作者一份, thank you!

#

#*********************************************************

if (!defined (' In_phpadleyiu '))

{

Exit (' Access Denied ');

}

/*

Instructions for use:

Declaring an upload class

Include_once (adleyliu_root. ") /inc_class.upload.php ');

$_yl_upload

= Array ();

$yl _uploadfile = new

Yl_upload_class ();

$_yl_upload[' yl_filedata '] =

' UploadFile ';//Table Single-name

$_yl_upload[' Yl_directroy '] =

' Upload_files '/upload home directory

$_yl_upload[' File_urldirectroy '] = '/';

Program Path

$_yl_upload[' yl_settingsnew '] =

". Date (' ym ')." /'. Date (' d '). ' /'. substr (Time (), 0,

5). ';/ /Upload the Child home directory

$_yl_upload[' yl_maxsize '] = 1048576;

Here in bytes (1024*2) *1024=2097152 is 2M

$_yl_upload[' yl_sizeformat '] =

' K '; Show file size unit b bytes, K thousand, M trillion

$_yl_upload[' yl_arrext '] =

Array (' gif ', ' jpg ', ' jpeg ', ' PNG ', ' bmp ', ' rar ', ' txt ');//Allow file types to be uploaded

$_yl_upload[' yl_ext '] = 0; 0 Original file type upload, 1 unified for Save as JPG

$_yl_upload[' yl_prefix ' = '. $uid. $yl _uploadfile-> Yl_createrand (1,0). $cid. ';

In the filename prefix plus special characters//$UID Member ID $cid category ID

$_yl_upload[' yl_suffix '] = '; '. $yl _uploadfile-> Yl_createrand (3,0).

Add special characters to the filename suffix

$_yl_upload[' thumbwidth '] = 100;

Thumbnail width

$_yl_upload[' thumbheight '] = 100;

Thumbnail height

$_yl_upload[' maxthumbwidth '] = 500;

Big picture Height

$_yl_upload[' maxthumbheight '] = 500;

Large Image width

Upload

$yl _uploadfile-> yl_uploadfile ();

Get Value:

' Yl_filename ' => addslashes ($_yl_upload[' Yl_filename '), original filename

' Yl_attachment ' => $_yl_upload[' yl_attachment '], new filename and path

' Yl_filesize ' => $_yl_upload[' yl_filesize ', file size

' Yl_filetype ' => $_yl_upload[' yl_filetype '], file type

' Yl_isimage ' => $_yl_upload[' yl_isimage ', whether it is a picture

' Yl_isthumb ' => $_yl_upload[' yl_isthumb '], is there a small picture

*/

Class Yl_upload_class

{

function __get ($property _name)

{

if (Isset ($this-> $property _name))

{

return $this-> $property _name;

} else

{

Return

NULL;

}

}

function __set ($property _name,

$value) {

$this-> $property _name =

$value;

}

#*********************************************************

#生成缩略图

#*********************************************************

function Makethumb ($srcfile) {

Global $_yl_upload;

To determine whether a file exists

if (!file_exists ($srcfile))

{

Return ";

}

$dstfile =

$srcfile. ' Small.jpg ';

$bigfile =

$srcfile. ' Big.jpg ';

Thumbnail size

$tow =

Intval ($_yl_upload[' thumbwidth '));

$toh =

Intval ($_yl_upload[' thumbheight '));

if ($tow <) $tow =

60;

if ($toh <) $toh = 60;

$make _max = 0;

$maxtow =

Intval ($_yl_upload[' maxthumbwidth '));

$maxtoh =

Intval ($_yl_upload[' maxthumbheight '));

if ($maxtow >= 300

&& $maxtoh >= 300) {

$make _max =

1;

}

Get picture information

$im = ';

if ($data =

GetImageSize ($srcfile)) {

if ($data [2] = = 1)

{

$make _max =

0;//gif not handle

if (function_exists ("Imagecreatefromgif"))

{

$im =

Imagecreatefromgif ($srcfile);

}

}

ElseIf ($data [2] = = 2)

{

if (function_exists ("Imagecreatefromjpeg"))

{

$im =

Imagecreatefromjpeg ($srcfile);

}

}

ElseIf ($data [2] = = 3)

{

if (function_exists ("Imagecreatefrompng"))

{

$im =

Imagecreatefrompng ($srcfile);

}

}

}

if (! $im)

Return ";

$SRCW = Imagesx ($im);

$srch = Imagesy ($im);

$TOWH = $tow/$toh;

$SRCWH =

$SRCW/$srch;

if ($towh <= $srcwh) {

$ftow =

$tow;

$ftoh = $ftow * ($srch/$SRCW);

$fmaxtow = $maxtow;

$fmaxtoh =

$fmaxtow * ($srch/$SRCW);

} else {

$ftoh =

$toh;

$ftow = $ftoh * ($SRCW/$srch);

$fmaxtoh = $maxtoh;

$fmaxtow =

$fmaxtoh * ($SRCW/$srch);

}

if ($SRCW <= $maxtow

&& $srch <= $maxtoh) {

$make _max =

0;//not handle

}

if ($SRCW > $tow $srch > $toh)

{

if (function_exists ("Imagecreatetruecolor") &&

Function_exists ("imagecopyresampled") && @ $ni =

Imagecreatetruecolor ($ftow, $ftoh))

{

Imagecopyresampled ($ni, $im, 0, 0, 0, 0, $ftow,

$ftoh, $SRCW,

$srch);

Big picture

if ($make _max

&& @ $maxni = Imagecreatetruecolor ($fmaxtow, $fmaxtoh))

{

Imagecopyresampled ($maxni, $im, 0, 0, 0, 0,

$fmaxtow, $fmaxtoh, $SRCW, $srch);

}else if (@ $maxni

= Imagecreatetruecolor (Round ($SRCW/2),

Round ($srch/2))) {

Imagecopyresampled ($maxni,

$im, 0, 0, 0, 0, round ($SRCW/2), round ($srch/2), $SRCW,

$srch);

}

}

ElseIf (function_exists ("Imagecreate") &&

Function_exists ("imagecopyresized") && @ $ni = imagecreate ($ftow, $ftoh))

{

Imagecopyresized ($ni, $im, 0, 0, 0, 0, $ftow,

$ftoh, $SRCW,

$srch);

Big picture

if ($make _max

&& @ $maxni = imagecreate ($fmaxtow, $fmaxtoh))

{

Imagecopyresized ($maxni, $im, 0, 0, 0, 0,

$fmaxtow, $fmaxtoh, $SRCW, $srch);

}else if (@ $maxni

= Imagecreate (Round ($SRCW/2),

Round ($srch/2))) {

Imagecopyresized ($maxni, $im,

0, 0, 0, 0, round ($SRCW/2), round ($srch/2), $SRCW,

$srch);

}

} else

{

Return

'';

}

if (function_exists (' imagejpeg '))

{

Imagejpeg ($ni,

$dstfile);

Big picture

if ($make _max)

{

Imagejpeg ($maxni,

$bigfile);

}else{

Imagejpeg ($maxni,

$bigfile);

}

}

ElseIf (function_exists (' imagepng ')) {

Imagepng ($ni,

$dstfile);

Big picture

if ($make _max)

{

Imagepng ($maxni,

$bigfile);

}else{

Imagejpeg ($maxni,

$bigfile);

}

}

Imagedestroy ($ni);

if ($make _max)

{

}else{

Imagedestroy ($MAXNI);

}

}else{

if (function_exists ("Imagecreatetruecolor") &&

Function_exists ("imagecopyresampled") && @ $ni =

Imagecreatetruecolor ($SRCW, $srch))

{

Imagecopyresampled ($ni, $im, 0, 0, 0, 0, $SRCW,

$ftoh, $srch,

$srch);

Big picture

$maxni =

Imagecreatetruecolor ($srch, $srch);

Imagecopyresampled ($maxni, $im, 0, 0, 0, 0, $SRCW, $srch, $SRCW,

$srch);

} elseif (Function_exists ("Imagecreate")

&& function_exists ("imagecopyresized") && @ $ni =

Imagecreate ($ftow, $ftoh)) {

Imagecopyresized ($ni,

$im, 0, 0, 0, 0, $SRCW, $srch, $SRCW,

$srch);

Big picture

$maxni =

Imagecreate ($fmaxtow, $fmaxtoh);

Imagecopyresized ($maxni, $im, 0, 0, 0, 0, $SRCW, $srch, $SRCW,

$srch);

} else

{

Return

'';

}

Imagejpeg ($ni, $dstfile);

Imagejpeg ($maxni,

$bigfile);

}

Imagedestroy ($im);

if (!file_exists ($dstfile)) {

Return

'';

} else {

Return

$dstfile;

}

}

#*********************************************************

#获取随机数函数

#*********************************************************

function

Yl_createrand ($length, $numeric = 0) {

Php_version < ' 4.2.0 '

&& Mt_srand (Double) microtime () * 1000000);

if ($numeric)

{

$hash = sprintf ('%0 '. $length. ' d ', Mt_rand (0, pow (10,

$length)-1));

} else {

$hash =

'';

$chars =

' ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ ';///0123456789

$max

= strlen ($chars)-1;

for ($i = 0; $i < $length; $i + +)

{

$hash. = $chars [Mt_rand (0,

$max)];

}

}

Return

$hash;

}

#***************

#*********************************************************

#创建目录函数

#*********************************************************

function

CreateFolder ($yl _path)

{

If

(!file_exists ($yl _path))

{

$this->

CreateFolder (dirname ($yl _path));

@mkdir ($yl _path,

0777);

}

return $this->

CreateFolder;

}

#*********************************************************

#获取文件

Name, size, type, temporary file name

#*********************************************************

function

Yl_getfilename ($yl _type)

{

Global

$_yl_upload;

Return

$_files[$_yl_upload[' Yl_filedata ']][$yl _type];

}

#*********************************************************

#获取文件大小

#*********************************************************

function

Yl_getfilesize ()

{

Global

$_yl_upload;

$yl _filesize = $this->

Yl_getfilename (' size ');

if ($yl _filesize = =

0) {

$this->

Alert ("Please select Upload file!");

Exit

}

if ($yl _filesize

> $_yl_upload[' yl_maxsize ']) {

Switch

(Strtolower ($_yl_upload[' Yl_sizeformat ')) {

Case

' B ':

$yl _maxsizek = $_yl_upload[' yl_maxsize '].

' B ';

Break

Case

' K ':

$yl _maxsizek =

$_yl_upload[' Yl_maxsize ']/1024. '

K ';

Break

Case

' m ':

$yl _maxsizek =

$_yl_upload[' Yl_maxsize ']/(1024*1024). '

M ';

}

$this->

Alert ("Upload file is out of bounds [". $yl _maxsizek. "]. K! ");

Exit

}

Return

$yl _filesize;

}

#*********************************************************

#获得文件扩展名

#*********************************************************

function

Yl_getfiletype ()

{

Global

$_yl_upload;

$pathinfo = PathInfo ($this-> yl_getfilename (' name '));

$yl _file_ext =

Strtolower ($pathinfo [' extension ']);

Check extension

if (!array_keys ($_yl_upload[' Yl_arrext '), $yl _file_ext))

{

$this->

Alert ("Upload file type is restricted!");

Exit

}

Return

$yl _file_ext;

}

#*********************************************************

#上传验证

#*********************************************************

function

Yl_upfile ($source, $target) {

//

If a function fails to upload, it can also be uploaded using other functions

if (function_exists (' Move_uploaded_file ')

&& @move_uploaded_file ($source, $target))

{

@chmod ($target, 0666);

Return

$target;

} elseif (@copy ($source, $target))

{

@chmod ($target, 0666);

Return

$target;

} elseif (@is_readable ($source))

{

if ($fp = @fopen ($source, ' RB '))

{

@flock ($FP, 2);

$filedata

=

@fread ($fp, @filesize ($source));

@fclose ($FP);

}

If

($fp = @fopen ($target, ' WB ') {

@flock ($FP,

2);

@fwrite ($FP,

$filedata);

@fclose ($FP);

@chmod

($target, 0666);

Return

$target;

} else {

Return

False

}

}

}

#*********************************************************

#上传

#*********************************************************

function

Yl_uploadfile ()

{

Global $_yl_upload;

$yl _file_path = $_yl_upload[' Yl_directroy ']. ' /'. $_yl_upload[' yl_settingsnew '];//Create a directory

$yl _filename = $this-> yl_getfilename (' name ');//original filename

$yl _filenamenews = $_yl_upload[' Yl_prefix '. SUBSTR (Time (), 5, 9). $_yl_upload[' Yl_suffix ']. /rename

$yl _file_size = $this-> yl_getfilesize ()//Get file size

$yl _file_type = $this-> yl_getfiletype ()//Get file type

if ($_yl_upload[' yl_ext ') = =

0) {

$yl _filenamenewsext = $yl _filenamenews. $yl _file_type;//renamed

}elseif ($_yl_upload[' yl_ext '] = = 1) {

$yl _filenamenewsext = $yl _filenamenews. JPG ';//unified renamed to JPG

}

$yl _tmp_name = Str_replace (",", $this->

Yl_getfilename (' tmp_name '))//temporary file name on the server

$yl _tmp_name = $this-> yl_getfilename (' tmp_name ');//temporary file name on server

Check if it has been uploaded

If<>< p=""> <>

Href= "! @is_uploaded_file ($yl _tmp_name" _cke_saved_href= "[email=! @is_uploaded_file ($yl _tmp_name]! @is_uploaded_ File ($yl _tmp_name ">mailto:! @is_uploaded_file ($yl _tmp_name" >! @is_uploaded_file ($yl _tmp_name))

{

$this->

Alert ("File uploaded!");

Exit

}

Check whether the directory exists or not, create

If<>< p=""> <>

Href= "! @is_dir (' $_yl_upload[' File_urldirectroy '). $yl _file_path. "_cke_saved_href=" [email=! @is_dir (]! @is_dir ('. $_yl_upload[' File_urldirectroy ']. $yl _file_path. ' " >mailto:! @is_dir ('. $_yl_upload[' File_urldirectroy '). $yl _file_path. ' " >! @is_dir ('. $_yl_upload[' File_urldirectroy '). $yl _file_path. ")

{

$this->

CreateFolder ('. $_yl_upload[' File_urldirectroy '). $yl _file_path. "); /create a table of contents

}

Check Directory Write permissions

If

(<>< p=""> <>

Href= "! @is_writable (' $_yl_upload[' File_urldirectroy '). $yl _file_path. "_cke_saved_href=" [email=! @is_writable (]! @is_writable ('. $_yl_upload[' File_urldirectroy ']. $yl _file_path. ' " >mailto:! @is_writable ('. $_yl_upload[' File_urldirectroy '). $yl _file_path. ' " >! @is_writable ('. $_yl_upload[' File_urldirectroy '). $yl _file_path. ")

{

$this->

Alert ("Upload directory does not write permission!");

Exit

}

$yl _path_name

=

'. $_yl_upload[' File_urldirectroy '. $yl _file_path. ' /'. $yl _filenamenewsext. ';

$yl _doupload = $this-> yl_upfile ($yl _tmp_name, $yl _path_name);

if ($yl _doupload = = False) {

$this-> alert ("Upload failed!");

Exit

}else{

Echo

' Upload success ';

Echo

'
';

/*

Echo

' Original filename: '. $yl _filename. ';

Echo

'
';

Echo

' New filename and directory: '. $yl _file_path. ' /'. $yl _filenamenewsext;

Echo

'
';

Echo

' File size: '. $yl _file_size. ';

Echo '
';

echo ' File type: '. $yl _file_type. ';

*/

$_yl_upload[' yl_filename ' = $yl _filename;

$_yl_upload[' yl_attachment ' = '. $yl _file_path. ' /'. $yl _filenamenewsext. ';

$_yl_upload[' yl_filesize ' = $yl _file_size;

$_yl_upload[' yl_filetype ' = $yl _file_type;

Check if picture

if (@getimagesize ($yl _path_name))

{

$_yl_upload[' yl_isimage '] =

1;

Generate thumbnails

if ($this-> makethumb ($yl _path_name)) {

$_yl_upload[' yl_isthumb '] = 1;

}else{

$_yl_upload[' yl_isthumb '] = 0;

}

}else{

$_yl_upload[' yl_isimage '] = 0;

}

}

Return

True

}

#*********************************************************

#提示

#*********************************************************

function

Alert ($yl _msg)

{

Echo ';

Echo ';

Echo ';

Echo ';

Echo ';

Echo ';

Echo ' <script></script> ';

Echo ';

Echo ';

Exit

}

}



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.