多檔案上傳類代碼_PHP教程

來源:互聯網
上載者:User
真正支援單檔案和多檔案上傳類代碼,修正了$_FILES[$field]['name']中的$field不能用變數只能和表單中的檔案名稱name="userfile"一致的缺點$_FILES['userfile']['name'],這裡 中的檔案名稱可以隨意取。

//index.htm
1、單檔案上傳

2、多檔案上傳

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

class File_upload{
public $upload_path='./upload/';//上傳檔案的路徑
public $allow_type=array();//允許上傳的檔案類型
public $max_size='20480';//允許的最大檔案大小
public $overwrite=false;//是否設定成覆蓋模式
public $renamed=false;//是否直接使用上傳檔案的名稱,還是系統自動命名

/**
* 私人變數
*/
private $upload_file=array();//儲存上傳成功檔案的資訊
private $upload_file_num=0;//上傳成功檔案的數目
private $succ_upload_file=array();//成功儲存的檔案資訊
/**
* 構造器
*
* @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();
}
/**
* 設定上傳路徑,並判定
*
* @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;
}
}
}
//設定上傳檔案類型
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);
}
}
//上傳檔案資料存放到數組中
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++;
}
}
}
/**
* 檢查上傳檔案是構滿足指定條件
*
*/
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;
//設定預設服務端檔案名稱
$this->upload_file[$i]['filename']=$this->upload_path.$this->upload_file[$i]['name'];
//擷取檔案路徑資訊
$file_info=pathinfo($this->upload_file[$i]['name']);
//擷取副檔名
$file_ext=$file_info['extension'];
//檢查檔案類型
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;$i<$this->upload_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'].' 出錯:'.$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'].' 出錯:'.$this->error($this->upload_file[$i]['error']).'!
';
}
}
echo $upload_msg;
}
//錯誤資訊
public function error($error)
{
switch ($error) {
case 1:
return '檔案大小超過php.ini 中 upload_max_filesize 選項限制的值';
break;
case 2:
return '檔案的大小超過了 HTML 表單中 MAX_FILE_SIZE 選項指定的值';
break;
case 3:
return '檔案只有部分被上傳';
break;
case 4:
return '沒有檔案被上傳';
break;
case 5:
return '這個檔案不允許被上傳';
break;
case 6:
return '檔案名稱為空白';
break;
default:
return '出錯';
break;
}
}
//擷取成功的資料資訊為數組(備用)
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);

?>


http://www.bkjia.com/PHPjc/445064.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/445064.htmlTechArticle真正支援單檔案和多檔案上傳類代碼,修正了$_FILES[ $field ]['name']中的$field不能用變數只能和表單中的檔案名稱name= userfile 一致的缺點$_FILES[...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.