Supports single-file and multi-file upload code, fixed the disadvantage of $ _ FILES [$ field] ['name'] That $ field cannot be used as a variable and can only be consistent with the name of the file in the form = "userfile". $ _ FILES ['userfile'] ['name'], the file name in <input type = "file" name = "userfile"> can be obtained at will.
// Index.htm
1. Upload a single file
<Form method = "post" action = "./upload. php" name = "frmUpload" enctype = "multipart/form-data">
<Input type = "file" name = "userfile" style = "WIDTH: 282px">
<Input type = "submit" align = "center" name = "upfiles" value = "OK"> </form>
2. Multifile upload
<Form method = "post" action = "./upload. php" name = "frmUpload" enctype = "multipart/form-data">
<Input type = "file" name = "userfile []" style = "WIDTH: 282px">
<Input type = "file" name = "userfile []" style = "WIDTH: 282px">
<Input type = "file" name = "userfile []" style = "WIDTH: 282px">
<Input type = "submit" align = "center" name = "upfiles" value = "OK">
</Form>
Slave --------------------------------------------------------------------------------------------------------------------------------
// Upload. php
<? Php
Class File_upload {
Public $ upload_path = './upload/'; // File upload path
Public $ allow_type = array (); // file type that can be uploaded
Public $ max_size = '000000'; // maximum file size allowed
Public $ overwrite = false; // whether to set it to overwrite Mode
Public $ renamed = false; // whether to use the name of the uploaded file directly or automatically
/**
* Private variables
*/
Private $ upload_file = array (); // Save the information of the uploaded file
Private $ upload_file_num = 0; // number of successfully uploaded files
Private $ succ_upload_file = array (); // information of the successfully saved file
/**
* Constructor
*
* @ Param string $ upload_path
* @ Param string $ allow_type
* @ Param string $ max_size
*/
Public function _ construct ($ upload_path = './upload/', $ allow_type = 'jpg | bmp | png | gif | jpeg ', $ max_size = '000000 ')
{
$ This-> set_upload_path ($ upload_path );
$ This-> set_allow_type ($ allow_type );
$ This-> max_size = $ max_size;
$ This-> get_upload_files ();
}
/**
* Set the upload path and determine
*
* @ Param string $ path
*/
Public function set_upload_path ($ path)
{
If (file_exists ($ path )){
If (is_writeable ($ path )){
$ This-> upload_path = $ path;
} Else {
If (@ chmod ($ path, '20140901 '))
$ This-> upload_path = $ path;
}
} Else {
If (@ mkdir ($ path, '20140901 ')){
$ This-> upload_path = $ path;
}
}
}
// Set the Upload File Type
Public function set_allow_type ($ types ){
$ This-> allow_type = explode ("|", $ types );
}
// Upload a file
Public function get_upload_files ()
{
Foreach ($ _ files as $ key => $ field)
{
$ This-> get_upload_files_detial ($ key );
}
}
// Upload File data and store it in an array
Public function get_upload_files_detial ($ field ){
If (is_array ($ _ FILES ["$ field"] ['name'])
{
For ($ I = 0; $ I <count ($ _ FILES [$ field] ['name']); $ I ++)
{
If (0 === _ FILES [$ field] ['error'] [$ I])
{
$ This-> upload_file [$ this-> upload_file_num] ['name'] =_ _ FILES [$ field] ['name'] [$ I];
$ This-> upload_file [$ this-> upload_file_num] ['type'] =_ _ FILES [$ field] ['type'] [$ I];
$ This-> upload_file [$ this-> upload_file_num] ['SIZE'] =_ _ FILES [$ field] ['SIZE'] [$ I];
$ This-> upload_file [$ this-> upload_file_num] ['tmp _ name'] = $ _ FILES [$ field] ['tmp _ name'] [$ I];
$ This-> upload_file [$ this-> upload_file_num] ['error'] = $ _ FILES [$ field] ['error'] [$ I];
$ This-> upload_file_num ++;
}
}
}
Else {
If (0 = $ _ FILES ["$ field"] ['error'])
{
$ This-> upload_file [$ this-> upload_file_num] ['name'] =$ _ FILES ["$ field"] ['name'];
$ This-> upload_file [$ this-> upload_file_num] ['type'] =_ _ FILES ["$ field"] ['type'];
$ This-> upload_file [$ this-> upload_file_num] ['SIZE'] = $ _ FILES ["$ field"] ['SIZE'];
$ This-> upload_file [$ this-> upload_file_num] ['tmp _ name'] = $ _ FILES ["$ field"] ['tmp _ name'];
$ This-> upload_file [$ this-> upload_file_num] ['error'] = $ _ FILES ["$ field"] ['error'];
$ This-> upload_file_num ++;
}
}
}
/**
* Check that the uploaded file meets the specified conditions.
*
*/
Public function check ($ I)
{
If (! Empty ($ this-> upload_file [$ I] ['name']) {
// Check the file size
If ($ this-> upload_file [$ I] ['SIZE']> $ this-> max_size * 1024) $ this-> upload_file [$ I] ['error'] = 2;
// Set the Default Server File Name
$ This-> upload_file [$ I] ['filename'] = $ this-> upload_path. $ this-> upload_file [$ I] ['name'];
// Obtain the file path information
$ File_info = pathinfo ($ this-> upload_file [$ I] ['name']);
// Obtain the file extension
$ File_ext = $ file_info ['extension'];
// Check the file type
If (! In_array ($ file_ext, $ this-> allow_type) $ this-> upload_file [$ I] ['error'] = 5;
// The name of the object to be renamed
If ($ this-> renamed ){
List ($ usec, $ sec) = explode ("", microtime ());
$ This-> upload_file [$ I] ['filename'] = $ sec. substr ($ usec, 2). '.'. $ file_ext;
Unset ($ usec );
Unset ($ sec );
}
// Check whether the file exists
If (file_exists ($ this-> upload_file [$ I] ['filename']) {
If ($ this-> overwrite ){
@ Unlink ($ this-> upload_file [$ I] ['filename']);
} Else {
$ J = 0;
Do {
$ J ++;
$ Temp_file = str_replace ('. '. $ file_ext ,'('. $ j. '). '. $ file_ext, $ this-> upload_file [$ I] ['filename']);
} While (file_exists ($ temp_file ));
$ This-> upload_file [$ I] ['filename'] = $ temp_file;
Unset ($ temp_file );
Unset ($ j );
}
}
// Check complete
} Else $ this-> upload_file [$ I] ['error'] = 6;
}
/**
* Upload a file
*
* @ Return true
*/
Public function upload ()
{
$ Upload_msg = '';
For ($ I = 0; $ I <$ this-> upload_file_num; $ I ++)
{
If (! Empty ($ this-> upload_file [$ I] ['name'])
{
// Check the file
$ This-> check ($ I );
If (0 = $ this-> upload_file [$ I] ['error'])
{
// Upload a file
If (! @ Move_uploaded_file ($ this-> upload_file [$ I] ['tmp _ name'], $ this-> upload_file [$ I] ['filename'])
{
$ Upload_msg. = 'upload file '. $ this-> upload_file [$ I] ['name']. 'error :'. $ this-> error ($ this-> upload_file [$ I] ['error']). '! <Br> ';
} Else
{
$ This-> succ_upload_file [] = $ this-> upload_file [$ I] ['filename'];
$ Upload_msg. = 'upload file'. $ this-> upload_file [$ I] ['name']. 'succeeded <br> ';
}
} Else $ upload_msg. = 'upload file '. $ this-> upload_file [$ I] ['name']. 'error :'. $ this-> error ($ this-> upload_file [$ I] ['error']). '! <Br> ';
}
}
Echo $ upload_msg;
}
// Error message
Public function error ($ error)
{
Switch ($ error ){
Case 1:
Return 'The file size exceeds the limit of the upload_max_filesize option in php. ini ';
Break;
Case 2:
Return 'The file size exceeds the value specified by the MAX_FILE_SIZE option in the HTML form ';
Break;
Case 3:
Return 'only part of the file is upload ';
Break;
Case 4:
Return 'no file is upload ';
Break;
Case 5:
Return 'this file cannot be upload ';
Break;
Case 6:
Return 'file name is null ';
Break;
Default:
Return 'error ';
Break;
}
}
// The retrieved data information is an array (optional)
Public function get_succ_file (){
Return $ this-> succ_upload_file;
}
}
$ Upload = new File_upload ('./upload/', 'jpg | bmp | png | gif | jpeg ');
$ Upload-> upload ();
$ T = $ upload-> get_succ_file ();
Print_r ($ t );
?>