PHP implementation of multiple image upload class instance

Source: Internet
Author: User
Tags file size file upload

This article mainly introduces the PHP implementation of multiple image upload class instances, the file annotation detailed function of the practical and has a certain degree of extensibility, the need for friends can refer to the

This article describes as a practical php multiple picture file upload class, its support GIF, JPG, JPEG, pjpeg, PNG format of multiple image upload function, class can also limit the type of picture, upload picture size, set up upload directory, some submit judgment and other functions. In addition, this class is not limited to the upload of pictures, you can upload txt/rar and other file types, just need to modify the code, interested readers can try their own.

PHP Multi-picture upload class complete function code is as follows:

  

Class more_file_upload{

Const FILE_PATH = '/uploadfile/'; Default File upload directory, please modify your own

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 is-> 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 exceeds 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.