国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php 多文件上傳的處理方法

2024-09-04 11:44:56
字體:
來源:轉載
供稿:網友

關于多文件上傳我們講過很多了,下面是一個簡單的普通form多文件上傳類了,需要的朋友可以下載使用一下,代碼如下:

  1. <?php 
  2.     
  3.        class upload { 
  4.              public $up_ext=array(), $up_max=5210, $up_dir
  5.              private $up_name$up_rename=true, $up_num=0, $up_files=array(), $up_ret=array(); 
  6.  
  7.              function __construct($name$ext=array(), $rename=true) { 
  8.                  if (!emptyempty($name)) { 
  9.                      $this->up_name = $name
  10.                      !emptyempty($ext) && $this->up_ext = $ext
  11.                      $this->up_rename = $rename
  12.                      $this->up_dir = website_dirroot. 
  13.                                      $globals['cfg_upload_path'];                 
  14.                      $this->initupload(); 
  15.                  } else { 
  16.                      exit('upload文件域名稱為空,初始化失??!'); 
  17.                  } 
  18.              } 
  19.  
  20.              private function initupload() { 
  21.                  if (is_array($_files[$this->up_name])) { 
  22.                      $up_arr = count($_files[$this->up_name]); 
  23.                      $up_all = count($_files[$this->up_name], 1); 
  24.                      $up_cnt = ($up_all - $up_arr) / $up_arr
  25.                      for ($i = 0; $i < $up_cnt$i ++) { 
  26.                           if ($_files[$this->up_name]['error'][$i] != 4) { 
  27.                               $this->up_files[] = array
  28.                                   'tmp_name' => $_files[$this->up_name]['tmp_name'][$i], 
  29.                                   'name' => $_files[$this->up_name]['name'][$i], 
  30.                                   'type' => $_files[$this->up_name]['type'][$i], 
  31.                                   'size' => $_files[$this->up_name]['size'][$i], 
  32.                                   'error' => $_files[$this->up_name]['error'][$i
  33.                               ); 
  34.                           } 
  35.                      } 
  36.                      $this->up_num = count($this->up_files); 
  37.                  } else { 
  38.                      if (isset($_files[$this->up_name])) { 
  39.                          $this->up_files = array
  40.                               'tmp_name' => $_files[$this->up_name]['tmp_name'], 
  41.                               'name' => $_files[$this->up_name]['name'], 
  42.                               'type' => $_files[$this->up_name]['type'], 
  43.                               'size' => $_files[$this->up_name]['size'], 
  44.                               'error' => $_files[$this->up_name]['error'
  45.                          ); 
  46.                          $this->up_num = 1; 
  47.                      } else { 
  48.                          exit('沒找找到需要upload的文件!'); 
  49.                      } 
  50.                  } 
  51.  
  52.                  $this->chkupload(); 
  53.              } 
  54.  
  55.              private function chkupload() { 
  56.                  if (emptyempty($this->up_ext)) { 
  57.                      $up_mime = array('image/wbmp''image/bmp''image/gif''image/pjpeg''image/x-png'); 
  58.                      foreach ($this->up_files as $up_file) { 
  59.                          $up_allw = false; 
  60.                          foreach ($up_mime as $mime) { 
  61.                              if ($up_file['type'] == $mime) { 
  62.                                  $up_allw = true; break
  63.                              } 
  64.                          }  
  65.                          !$up_allw && exit('不允許上傳'.$up_file['type'].'格式的文件!'); 
  66.  
  67.                          if ($up_file['size'] / 1024 > $this->up_max) { 
  68.                              exit('不允許上傳大于 '.$this->up_max.'k 的文件!'); 
  69.                          } 
  70.                      }  
  71.                  } else { 
  72.                      foreach ($this->up_files as $up_file) { 
  73.                          $up_ext = end(explode('.'$up_file['name'])); 
  74.  
  75.                          $up_allw = false; 
  76.                          foreach ($this->up_ext as $ext) {                              
  77.                              if ($up_ext == $ext) { 
  78.                                  $up_allw = true; break
  79.                              } 
  80.                          } 
  81.                          !$up_allw && exit('不允許上傳.'.$up_ext.'格式的文件!'); 
  82.  
  83.                          if ($up_file['size'] / 1024 > $this->up_max) { 
  84.                              exit('不允許上傳大于 '.$this->up_max.'k 的文件!'); 
  85.                          } 
  86.                      } 
  87.                  } 
  88.  
  89.                  $this->uploading(); 
  90.              } 
  91.  
  92.              private function uploading() { 
  93.                  if (io::dircreate($this->up_dir)) { 
  94.                      if (chmod($this->up_dir, 0777)) { 
  95.                          if (!emptyempty($this->up_files)) { 
  96.                              foreach ($this->up_files as $up_file) { 
  97.                                  if (is_uploaded_file($up_file['tmp_name'])) { 
  98.                                      $file_name = $up_file['name']; 
  99.                                      if ($this->up_rename) { 
  100.                                          $file_ext = end(explode('.'$file_name)); 
  101.                                          $file_rnd = substr(md5(uniqid()), mt_rand(0, 26), 6);  
  102.                                          $file_name = date('ymdhis').'_'.$file_rnd.'.'.$file_ext
  103.                                      }  
  104.                                      $file_name = $this->up_dir.'/'.$file_name
  105.  
  106.                                      if (move_uploaded_file($up_file['tmp_name'], $file_name)) { 
  107.                                          $this->up_ret[] = str_replace(website_dirroot, ''$file_name); 
  108.                                      } else { 
  109.                                          exit('文件上傳失敗!'); 
  110.                                      } 
  111.                                  } 
  112.                              } 
  113.                          } 
  114.                      } else { 
  115.                          exit('未開啟寫入權限!'); 
  116.                      } 
  117.                  } else { 
  118.                      exit('上傳目錄創建失??!'); 
  119.                  } 
  120.              } 
  121.  
  122.              public function getupload() { 
  123.                  return emptyempty($this->up_ret) ? false : $this->up_ret; 
  124.              } 
  125.  
  126.              function __destruct() {} 
  127.        } 
  128. ?> 

大量文件或大體積文件的情況可以考慮調用組件解決(如前文提到的swfupload組件),然而有些情況只需要傳遞幾個文件,而且文件體積并不太大,這種情況下使用組件則有點牛刀殺雞的感覺,通過html自帶的<input type="file">表單就可以實現需要的功能,關鍵在于后臺接收程序的處理.

php處理上傳做的很方便,上傳文件的信息通過服務器自動處理到$_files數組中,開發者只需要使用的內置處理函數簡單操作就可以啦。asp教程開發者則沒有這么幸運,官方并沒有提供直接的處理方法,需要開發者自己設計,這時就需要開發者了解iis對enctype="multipart/form-data"表單的處理方式,iis把enctype="multipart/form-data"表單提交的數據存儲成二進制數據,以二進制格式返回給開發者,開發者則需要通過lenb、midb的字節處理函數來分析獲取的上傳內容,客戶端發送的具體表單數據格式,可以了解下http rfc1867協議傳輸格式方面的知識.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 滨海县| 错那县| 怀来县| 荔浦县| 益阳市| 长宁区| 北碚区| 池州市| 原平市| 六枝特区| 鱼台县| 和林格尔县| 安徽省| 永仁县| 乌兰浩特市| 黑龙江省| 新巴尔虎右旗| 罗山县| 南召县| 洞口县| 台州市| 甘泉县| 县级市| 马龙县| 恩施市| 西丰县| 乐山市| 仲巴县| 衡阳市| 高阳县| 凤翔县| 武定县| 兴业县| 南川市| 南木林县| 万全县| 墨竹工卡县| 伊川县| 岐山县| 岐山县| 南郑县|