PHP multiple File Upload class

Source: Internet
Author: User
Tags file upload

PHP multiple File Upload class

/*

Multi-File Upload class

Modified: Linvo 2008-2-15

*/

Class more_file_upload{

Const file_path= '.. /upfileclass/uploadfile/';

var File_type;

var File_type_array;

var File_type_real_array;

var file_type_string;

var file_name;

var file_size;

var file_tmp_name;

var file_error;

var handledate;

Static totalsize=0;

function __construct (file_name,file_error,file_size,file_tmp_name,file_type) {

This->handledate=date (' m-d-y ');

if (!empty (file_name)) {

This->file_name = file_name;

This->file_error = File_error;

This->file_size = file_size;

This->file_tmp_name = File_tmp_name;

This->file_type = File_type;

This->file_type_array = Array ('/', ' image/gif ', ' image/jpg ', ' image/jpeg ', ' image/pjpeg ', ' image/png ');

This->file_type_real_array = Array (0.1, ' jpg ' =>74707370, ' gif ' =>7173, ' bmp ' =>6677, ' png ' =>807871);

This->show_execute_message (this->file_error,this->file_name,this->file_type,this->file_size);

}

}

function __destruct () {

This->file_name = NULL;

This->file_error = NULL;

This->file_size = NULL;

This->file_tmp_name = NULL;

This->file_type = NULL;

self::totalsize = 0;

}

function Show_execute_message (smfileerror,smfilename,smfiletype,smfilesize) {

if (smfileerror>0) {

Switch (smfileerror) {

Case 1:smfilemessage= ' file exceeds server's convention size!' Break

Case 2:smfilemessage= ' file is larger than the specified file size!' Break

Case 3:smfilemessage= ' file only uploaded part!' Break

Case 4:echo "This->file_name". File upload failed!
'; break;

}

Self::__destruct ();

}else{

Smfiletypeflag = Array_search (Smfiletype,this->file_type_array);

For real-world format validation

if (Smfiletypeflag!= false) {

File = fopen (This->file_tmp_name, "RB");

Bin = fread (file, 10);

fclose (file);

Strinfo = @unpack ("C10chars", bin);

TypeCode = intval (strinfo[' chars1 '].strinfo[' chars2 '));

Smfiletypeflag = Array_search (TypeCode, This->file_type_real_array);

if (Smfiletypeflag = = False) {//To determine whether a PNG picture

TypeCode = intval (strinfo[' chars2 '].strinfo[' chars3 '].strinfo[' chars4 ']);

Smfiletypeflag = Array_search (TypeCode, This->file_type_real_array);

if (Smfiletypeflag = = False) {//To determine whether a JPG picture

TypeCode = intval (strinfo[' chars7 '].strinfo[' chars8 '].strinfo[' "Chars9 '].strinfo[' Chars10 ')");

Smfiletypeflag = Array_search (TypeCode, This->file_type_real_array);

}

}

}

if (Smfiletypeflag = = False) {

Smfilemessage= ' file type is wrong, please verify!'

Self::__destruct ();

}else{

Resflag = This->move_file (this->file_tmp_name,this->file_name);

if (Resflag = = 1) {

smfilemessage = ' File upload success! '

Self::totalsize + + intval (smfilesize);

Self::__destruct ();

}else{

Smfilemessage = ' file upload failed!'

Self::__destruct ();

}

}

}

Smfilesizeformat = THIS->SIZE_BKM (smfilesize);

Echo '

'. Smfilename. '

'. Smfiletype. '

'. Smfilesizeformat. '

'. Smfilemessage. '

';

}

function Move_file (mvfiletmp,mvfilename) {//Move file

Mvfilenamearr = Explode ('. ', basename (mvfilename));

Mvfilenamearr[0] = this->rand_string ();

Mvfilename = Implode ('. ', Mvfilenamearr);

if (Is_uploaded_file (mvfiletmp)) {

UploadFile = Self::file_path. " Mvfilename ";

result = Move_uploaded_file (mvfiletmp,uploadfile);

return result;

}

}

function rand_string () {

string = MD5 (Uniqid (rand (). Microtime ()));

return string;

}

function size_bkm (size) {//B/KB/MB unit conversion

if (Size < 1024)

{

SIZE_BKM = (string) size. "B";

}

ElseIf (Size < (1024 * 1024))

{

SIZE_BKM = Number_format (double) (size/1024), 1). "KB";

}else

{

SIZE_BKM = Number_format (double) (Size/(1024*1024)), 1. " MB ";

}

return size_bkm;

}

}

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.