Home  >  Article  >  Backend Development  >  Multiple file upload class code_PHP tutorial

Multiple file upload class code_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:05:59802browse

True support for single file and multi-file upload code, corrected $_FILES[$field]['name'] cannot be used with variables and can only be used with forms The file name in name="userfile" is consistent with the disadvantage $_FILES['userfile']['name'], where The file name in "> can be chosen as desired.

//index.htm
1. Single file upload




2. Multiple file upload
< form method="post" action="./upload.php" name="frmUpload" enctype="multipart/form-data" >





---------------------------------------------------------------- -------------------------------------------------- --------------------------
//upload.php

class File_upload{
public $upload_path='./upload/';//The path to the uploaded file
public $allow_type=array();//The file types allowed to be uploaded
public $max_size='20480';//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 is it automatically named by the system

/**
* Private variables
*/
private $upload_file=array();//Save the information of successfully uploaded files
private $upload_file_num=0;//The number of successfully uploaded files
private $succ_upload_file=array();//Successfully saved file information
/**
* 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='204800')
{
$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,'0666'))
$this->upload_path=$path;
}
}else{
if(@mkdir($path,'0666')){
$this->upload_path=$path;
}
}
}
//Set the upload file type
public function set_allow_type($types){
$this- >allow_type=explode("|",$types);
}
//Upload files
public function get_upload_files()
{
foreach ($_FILES AS $key=> $field)
{
$this->get_upload_files_detial($key);
}
}
//The uploaded file data is stored in the array
public function get_upload_files_detial($field ){
if(is_array($_FILES["$field"]['name']))
{
for($i=0;$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 whether the uploaded file meets the specified conditions
*
*/
public function check($i)
{
if(!empty($this->upload_file[$i]['name'])){
//Check 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'];
//Get file path information
$file_info=pathinfo($this->upload_file[$i]['name']);
//Get file extension
$file_ext=$file_info['extension'];
//Check file type
if(!in_array($file_ext,$this->allow_type))$this->upload_file[$i ]['error']=5;
//Need 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 completed
} else $this->upload_file[$i]['error']=6;
}
/**
* Upload 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 file
$this- >check($i);
if (0==$this->upload_file[$i]['error'])
{
//Upload 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']).'!
';
}else
{
$this->succ_upload_file[]=$this->upload_file[$i]['filename'] ;
$upload_msg.='Upload file'.$this->upload_file[$i]['name'].' Successful
';
}
}else $upload_msg. ='Upload file'.$this->upload_file[$i]['name'].' Error:'.$this->error($this->upload_file[$i]['error']) .'!
';
}
}
echo $upload_msg;
}
//Error message
public function error($error)
{
switch ($error) {
case 1:
return 'The file size exceeds the value limited by the upload_max_filesize option in php.ini';
break;
case 2:
return 'File The size exceeds the value specified by the MAX_FILE_SIZE option in the HTML form';
break;
case 3:
return 'Only part of the file was uploaded';
break;
case 4:
return 'No file was uploaded';
break;
case 5:
return 'This file is not allowed to be uploaded';
break;
case 6:
return 'File Named empty';
break;
default:
return 'Error';
break;
}
}
//The successfully obtained data information is an array (backup )
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);

?>


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445064.htmlTechArticleTruly supports single file and multi-file upload code, corrected $_FILES[ $field ]['name'] The $field in cannot be used as a variable and can only be consistent with the file name name= userfile in the form. The disadvantage is $_FILES[...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn