Php classic File Upload class-PHP source code

Source: Internet
Author: User
Ec (2 ); php classic File Upload class #********************************** * ********************** # File Name: inc_class.upload.php # Function Description: File Upload class # Program Creation: (adleyliu) # contact qq & nbsp;: 143390952.0.a # Note: Keep this declaration information during forwarding, this statement does not affect your speed of script ec (2); script

Php classic File Upload class
#*************************************** ******************
# File Name:
Inc_class.upload.php
# Function Description: upload a file
# Program Production: adleyliu)
# Contact qq
14339095

2.0.a
# Note: Please keep this statement information during forwarding. This statement does not affect your speed!
# If there is any modification, please send the modified file to the author by email. Thank you!
#
#*************************************** ******************
If (! Defined ('in _ PHPADLEYIU '))
{
Exit ('Access Denied ');
}

/*
// Instructions for use:
// Declare 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'; // form name
$ _ YL_UPLOAD ['yl _ directroy '] =
'Upload _ files '; // upload the main directory
$ _ YL_UPLOAD ['file _ urldirectroy '] = '/';//
Program path
$ _ YL_UPLOAD ['yl _ settingsnew'] =
''. Date ('ymm'). '/'. date ('D'). '/'. substr (time (), 0,
5). ''; // upload the sub-main directory
$ _ YL_UPLOAD ['yl _ maxsize '] = 1048576;
// Here the unit is byte (1024*2) * 1024 = 2097152 is 2 M
$ _ YL_UPLOAD ['yl _ sizeformat '] =
'K'; // the size of the display file, in bytes B, k, m MB
$ _ YL_UPLOAD ['yl _ arrext '] =
Array ('gif', 'jpg ', 'jpeg', 'png ', 'bmp', 'rar ', 'txt'); // specifies the file type that can be uploaded.
$ _ YL_UPLOAD ['yl _ ext ']
= 0; // 0 original file type upload, 1 is saved as jpg
$ _ YL_UPLOAD ['yl _ prefix'] =
''. $ Uid.''. $ yl_uploadfile-> yl_createrand (1, 0). ''. $ cid .'';
// Add special characters to the file name prefix // $ 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;
// Large Image Height
$ _ YL_UPLOAD ['maxthumbheight'] = 500;
// Large image width
// Upload
$ Yl_uploadfile-> yl_uploadfile ();
Get value:
'Yl _ filename'
=> Addslashes ($ _ YL_UPLOAD ['yl _ filename']), original file name
'Yl _ attachment '=>
$ _ YL_UPLOAD ['yl _ attachment '], new file name 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 an image
'Yl _ isthumb' =>
$ _ YL_UPLOAD ['yl _ isthumb']. Is there any small image?
*/

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;
}

#*************************************** ******************
# Generating thumbnails
#*************************************** ******************

Function makethumb ($ srcfile ){
Global $ _ YL_UPLOAD;

// 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 <60) $ tow =
60;
If ($ toh <60) $ toh = 60;

$ Make_max = 0;
$ Maxtow =
Intval ($ _ YL_UPLOAD ['maxthumbwidth']);
$ Maxtoh =
Intval ($ _ YL_UPLOAD ['maxthumbheight']);
If ($ maxtow & gt; = 300
& $ Maxtoh> = 300 ){
$ Make_max =
1;
}

// Obtain image information
$ Im = '';
If ($ data =
Getimagesize ($ srcfile )){
If ($ data [2] = 1)
{
$ Make_max =
0; // gif is not processed
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 processed
}
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 );
// Large image
If ($ make_max
& Amp; @ $ 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 );
// Large image
If ($ make_max
& Amp; @ $ 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 );
// Large image
If ($ make_max)
{
Imagejpeg ($ maxni,
$ Bigfile );
} Else {
Imagejpeg ($ maxni,
$ Bigfile );
}
}
Elseif (function_exists ('imagepng ')){
Imagepng ($ ni,
$ Dstfile );
// Large image
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 );
// Large image
$ 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 );
// Large image
$ 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;
}
}

#*************************************** ******************
# Random number Acquisition Function
#*************************************** ******************
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 =
'Abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxy'; // 0123456789
$ Max
= Strlen ($ chars)-1;
For ($ I = 0; $ I <$ length; $ I ++)
{
$ Hash. = $ chars [mt_rand (0,
$ Max)];
}
}
Return
$ Hash;
}
#***************
#*************************************** ******************
# Create a directory Function
#*************************************** ******************
Function
Createfolder ($ yl_path)
{
If
(! File_exists ($ yl_path ))
{
$ This->
Createfolder (dirname ($ yl_path ));
@ Mkdir ($ yl_path,
0777 );
}
Return $ this->
Createfolder;
}
#*************************************** ******************
# Getting files
Name, size, type, temporary file name
#*************************************** ******************
Function
Yl_getfilename ($ yl_type)
{
Global
$ _ YL_UPLOAD;
Return
$ _ FILES [$ _ YL_UPLOAD ['yl _ filedata'] [$ yl_type];
}
#*************************************** ******************
# Getting the file size
#*************************************** ******************
Function
Yl_getfilesize ()
{
Global
$ _ YL_UPLOAD;
$ Yl_filesize = $ this->
Yl_getfilename ('SIZE ');
If ($ yl_filesize =
0 ){
$ This->
Alert ("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 ("the uploaded file exceeds the limit [". $ yl_maxsizek. "]. K! ");
Exit;
}
Return
$ Yl_filesize;
}
#*************************************** ******************
# Obtain the file extension
#*************************************** ******************
Function
Yl_getfiletype ()
{
Global
$ _ YL_UPLOAD;
$ Pathinfo = pathinfo ($ this->
Yl_getfilename ('name '));
$ Yl_file_ext =
Strtolower ($ pathinfo ['extension']);
// Check the extension
If (! Array_keys ($ _ YL_UPLOAD ['yl _ arrext '], $ yl_file_ext ))
{
$ This->
Alert ("the upload file type is limited! ");
Exit;
}
Return
$ Yl_file_ext;
}

#*************************************** ******************
# Upload Verification
#*************************************** ******************
Function
Yl_upfile ($ source, $ target ){
//
If a function fails to be uploaded, you can use another function
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;
}
}
}
#*************************************** ******************
# Upload
#*************************************** ******************
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 file name
$ Yl_filenamenews =
$ _ YL_UPLOAD ['yl _ prefix']. ''. substr (time (), 5,
9). ''. $ _ YL_UPLOAD ['yl _ suffix '].''; // rename
$ Yl_file_size = $ this
-> Yl_getfilesize (); // obtain the file size
$ Yl_file_type = $ this->
Yl_getfiletype (); // obtain the file type
If ($ _ YL_UPLOAD ['yl _ ext '] =
0 ){
$ Yl_filenamenewsext =
$ Yl_filenamenews. '.'. $ yl_file_type; // rename
} Elseif
($ _ YL_UPLOAD ['yl _ ext '] = 1 ){
$ Yl_filenamenewsext =
$Yl_filenamenews.'.jpg '; // you can change the name 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 the server
// Check whether the object has been uploaded
If (href = "mailto :! @ Is_uploaded_file ($ yl_tmp_name ">! @ Is_uploaded_file

($ Yl_tmp_name ))
{
$ This->
Alert ("File Uploaded! ");
Exit;
}
// Check whether the directory exists. If the directory does not exist, create
If (href = "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

Directory
}
// Check the directory write permission
If
(Href = "mailto :! @ Is_writable (''. $ _ YL_UPLOAD

['File _ urldirectroy ']. ''. $ yl_file_path.'' ">! @ Is_writable (''. $ _ YL_UPLOAD

['File _ urldirectroy ']. ''. $ yl_file_path .''))
{
$ This->
Alert ("the upload directory has no 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 successful ';
// Echo
'
';
/*
Echo
'Original file name: '. $ yl_filename .'';
Echo
'
';
Echo
'New file name 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 for images
If (@ getimagesize ($ yl_path_name ))
{
$ _ YL_UPLOAD ['yl _ isimage'] =
1;
/// Generate a thumbnail
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;
}

#*************************************** ******************
# Prompt
#*************************************** ******************
Function
Alert ($ yl_msg)
{
Echo
'';
Echo'';
Echo
'Error';
Echo'
Http-equiv = "content-type" content = "text/html;
Charset = gb2312 "> ';
Echo'';
Echo
'';
Echo'Type = "text/javascript"> alert ("'. $ yl_msg.'"); history. back (); 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.