日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > php >内容正文

php

php文件多上传文件,php文件上传(多文件上传)

發(fā)布時(shí)間:2023/12/9 php 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php文件多上传文件,php文件上传(多文件上传) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

http://www.cnblogs.com/itcx/p/4209034.html

upload.php

class File_upload{

public $upload_path='./upload/';//上傳文件的路徑

public $allow_type=array();//允許上傳的文件類(lèi)型

public $max_size='20480';//允許的最大文件大小

public $overwrite=false;//是否設(shè)置成覆蓋模式

public $renamed=false;//是否直接使用上傳文件的名稱(chēng),還是系統(tǒng)自動(dòng)命名

/**

* 私有變量

*/

private $upload_file=array();//保存上傳成功文件的信息

private $upload_file_num=0;//上傳成功文件的數(shù)目

private $succ_upload_file=array();//成功保存的文件信息

/**

* 構(gòu)造器

*

* @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();

}

/**

* 設(shè)置上傳路徑,并判定

*

* @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;

}

}

}

//設(shè)置上傳文件類(lèi)型

public function set_allow_type($types){

$this->allow_type=explode("|",$types);

}

//上傳文件

public function get_upload_files()

{

foreach ($_FILES AS $key=>$field)

{

$this->get_upload_files_detial($key);

}

}

//上傳文件數(shù)據(jù)存放到數(shù)組中

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++;

}

}

}

/**

* 檢查上傳文件是構(gòu)滿足指定條件

*

*/

public function check($i)

{

if(!empty($this->upload_file[$i]['name'])){

//檢查文件大小

if($this->upload_file[$i]['size']>$this->max_size*1024)$this->upload_file[$i]['error']=2;

//設(shè)置默認(rèn)服務(wù)端文件名

$this->upload_file[$i]['filename']=$this->upload_path.$this->upload_file[$i]['name'];

//獲取文件路徑信息

$file_info=pathinfo($this->upload_file[$i]['name']);

//獲取文件擴(kuò)展名

$file_ext=$file_info['extension'];

//檢查文件類(lèi)型

if(!in_array($file_ext,$this->allow_type))$this->upload_file[$i]['error']=5;

//需要重命名的

if($this->renamed){

list($usec, $sec) = explode(" ",microtime());

$this->upload_file[$i]['filename']=$sec.substr($usec,2).'.'.$file_ext;

unset($usec);

unset($sec);

}

//檢查文件是否存在

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);

}

}

//檢查完畢

} else $this->upload_file[$i]['error']=6;

}

/**

* 上傳文件

*

* @return true

*/

public function upload()

{

$upload_msg='';

for($i=0;$iupload_file_num;$i++)

{

if(!empty($this->upload_file[$i]['name']))

{

//檢查文件

$this->check($i);

if (0==$this->upload_file[$i]['error'])

{

//上傳文件

if(!@move_uploaded_file($this->upload_file[$i]['tmp_name'],$this->upload_file[$i]['filename']))

{

$upload_msg.='上傳文件'.$this->upload_file[$i]['name'].' 出錯(cuò):'.$this->error($this->upload_file[$i]['error']).'!
';

}else

{

$this->succ_upload_file[]=$this->upload_file[$i]['filename'];

$upload_msg.='上傳文件'.$this->upload_file[$i]['name'].' 成功了
';

}

}else $upload_msg.='上傳文件'.$this->upload_file[$i]['name'].' 出錯(cuò):'.$this->error($this->upload_file[$i]['error']).'!
';

}

}

echo $upload_msg;

}

//錯(cuò)誤信息

public function error($error)

{

switch ($error) {

case 1:

return '文件大小超過(guò)php.ini 中 upload_max_filesize 選項(xiàng)限制的值';

break;

case 2:

return '文件的大小超過(guò)了 HTML 表單中 MAX_FILE_SIZE 選項(xiàng)指定的值';

break;

case 3:

return '文件只有部分被上傳';

break;

case 4:

return '沒(méi)有文件被上傳';

break;

case 5:

return '這個(gè)文件不允許被上傳';

break;

case 6:

return '文件名為空';

break;

default:

return '出錯(cuò)';

break;

}

}

//獲取成功的數(shù)據(jù)信息為數(shù)組(備用)

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);

轉(zhuǎn)載于:https://www.cnblogs.com/itcx/p/4209034.html

標(biāo)簽:文件,name,upload,field,file,error,path,php,上傳

來(lái)源: https://blog.csdn.net/weixin_30338497/article/details/95934199

總結(jié)

以上是生活随笔為你收集整理的php文件多上传文件,php文件上传(多文件上传)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。