This article mainly introduces the PHP multifile Upload class. the example analyzes the principles and implementation skills of php for multifile upload, which has some reference value, for more information about PHP multifile Upload, see the following example. Share it with you for your reference. The details are as follows:
The code is as follows:
<? Php
/*
PHP multi-file Upload class
Modify: Linvo
*/
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;
Var $ 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;
$ This-> $ totalsize = 0;
}
Function show_execute_message ($ smfileerror, $ smfilename, $ smfiletype, $ smfilesize ){
If ($ smfileerror> 0 ){
Switch ($ smfileerror ){
Case 1: $ smfilemessage ='The file size exceeds the server's agreed size!'; Break;
Case 2: $ smfilemessage ='The file size exceeds the specified size!'; Break;
Case 3: $ smfilemessage ='Only part of the file is uploaded!'; Break;
Case 4: echo "$ this-> $ file_name". 'file Upload failed!
'; Break;
}
Self: :__ destruct ();
} Else {
$ Smfiletypeflag = array_search ($ smfiletype, $ this-> $ file_type_array );
// Verify the real format
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) {// determines whether the image is a png image.
$ TypeCode = intval ($ strInfo ['chars2 ']. $ strInfo ['chars3']. $ strInfo ['charss']);
$ Smfiletypeflag = array_search ($ typeCode, $ this-> $ file_type_real_array );
If ($ smfiletypeflag = false) {// you can determine whether the image is a jpg image.
$ TypeCode = intval ($ strInfo ['chars7 ']. $ strInfo ['charss']. $ strInfo ['chars9']. $ strInfo ['chars10']);
$ Smfiletypeflag = array_search ($ typeCode, $ this-> $ file_type_real_array );
}
}
}
If ($ smfiletypeflag = false ){
$ Smfilemessage ='Incorrect file type. check the file!';
Self: :__ destruct ();
} Else {
$ Resflag =$ $ this-> move_file ($ this-> $ file_tmp_name, $ this-> $ file_name );
If ($ resflag = 1 ){
$ Smfilemessage = 'File uploaded successfully! ';
$ This-> 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 the 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 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;
}
}
?>
I hope this article will help you with php programming.