亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 語言 > PHP > 正文

實現多文件上傳php類

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

多文件上傳是PHP中的一個基礎應用,反正PHPer都會遇到的問題,現在就介紹一個功能完善、強大的多文件上傳類給大家吧,能用上這個類的地方會很多,代碼如下:

  1. <?php 
  2. class Upload{ 
  3.  var $saveName;// 保存名 
  4.  var $savePath;// 保存路徑 
  5.  var $fileFormat = array('gif','jpg','doc','application/octet-stream');// 文件格式&MIME限定 
  6.  var $overwrite = 0;// 覆蓋模式 
  7.  var $maxSize = 0;// 文件最大字節 
  8.  var $ext;// 文件擴展名 
  9.  var $thumb = 0;// 是否生成縮略圖 
  10.  var $thumbWidth = 130;// 縮略圖寬 
  11.  var $thumbHeight = 130;// 縮略圖高 
  12.  var $thumbPrefix = "_thumb_";// 縮略圖前綴 
  13.  var $errno;// 錯誤代號 
  14.  var $returnArrayarray();// 所有文件的返回信息 
  15.  var $returninfoarray();// 每個文件返回信息 
  16.  
  17. // 構造函數 
  18. // @param $savePath 文件保存路徑 
  19. // @param $fileFormat 文件格式限制數組 
  20. // @param $maxSize 文件最大尺寸 
  21. // @param $overwriet 是否覆蓋 1 允許覆蓋 0 禁止覆蓋 
  22.  function Upload($savePath$fileFormat='',$maxSize = 0, $overwrite = 0) { 
  23.   $this->setSavepath($savePath); 
  24.   $this->setFileformat($fileFormat); 
  25.   $this->setMaxsize($maxSize); 
  26.   $this->setOverwrite($overwrite); 
  27.   $this->setThumb($this->thumb, $this->thumbWidth,$this->thumbHeight); 
  28.   $this->errno = 0; 
  29.  } 
  30. // 上傳 
  31. // @param $fileInput 網頁Form(表單)中input的名稱 
  32. // @param $changeName 是否更改文件名 
  33.  function run($fileInput,$changeName = 1){ 
  34.   if(isset($_FILES[$fileInput])){ 
  35.    $fileArr = $_FILES[$fileInput]; 
  36.    if(is_array($fileArr['name'])){//上傳同文件域名稱多個文件 
  37.     for($i = 0; $i < count($fileArr['name']); $i++){ 
  38.      $ar['tmp_name'] = $fileArr['tmp_name'][$i]; 
  39.      $ar['name'] = $fileArr['name'][$i]; 
  40.      $ar['type'] = $fileArr['type'][$i]; 
  41.      $ar['size'] = $fileArr['size'][$i]; 
  42.      $ar['error'] = $fileArr['error'][$i]; 
  43.      $this->getExt($ar['name']);//取得擴展名,賦給$this->ext,下次循環會更新 
  44.      $this->setSavename($changeName == 1 ? '' : $ar['name']);//設置保存文件名 
  45.      if($this->copyfile($ar)){ 
  46.       $this->returnArray[] =  $this->returninfo; 
  47.      }else
  48.       $this->returninfo['error'] = $this->errmsg(); 
  49.       $this->returnArray[] =  $this->returninfo; 
  50.      } 
  51.     } 
  52.     return $this->errno ?  false :  true; 
  53.    }else{//上傳單個文件 
  54.     $this->getExt($fileArr['name']);//取得擴展名 
  55.     $this->setSavename($changeName == 1 ? '' : $fileArr['name']);//設置保存文件名 
  56.     if($this->copyfile($fileArr)){ 
  57.      $this->returnArray[] =  $this->returninfo; 
  58.     }else
  59.      $this->returninfo['error'] = $this->errmsg(); 
  60.      $this->returnArray[] =  $this->returninfo; 
  61.     } 
  62.     return $this->errno ?  false :  true; 
  63.    } 
  64.    return false; 
  65.   }else
  66.    $this->errno = 10; 
  67.    return false; 
  68.   } 
  69.  } 
  70. // 單個文件上傳 
  71. // @param $fileArray 文件信息數組 
  72.  function copyfile($fileArray){ 
  73.   $this->returninfo = array(); 
  74.   // 返回信息 
  75.   $this->returninfo['name'] = $fileArray['name']; 
  76.   $this->returninfo['md5'] = @md5_file($fileArray['tmp_name']); 
  77.   $this->returninfo['saveName'] = $this->saveName; 
  78.   $this->returninfo['size'] = number_format( ($fileArray['size'])/1024 , 0, '.'' ');//以KB為單位 
  79.   $this->returninfo['type'] = $fileArray['type']; 
  80.   // 檢查文件格式 
  81.   if (!$this->validateFormat()){ 
  82.    $this->errno = 11; 
  83.    return false; 
  84.   } 
  85.   // 檢查目錄是否可寫 
  86.   if(!@is_writable($this->savePath)){ 
  87.    $this->errno = 12; 
  88.    return false; 
  89.   } 
  90.   // 如果不允許覆蓋,檢查文件是否已經存在 
  91.   //if($this->overwrite == 0 && @file_exists($this->savePath.$fileArray['name'])){ 
  92.   // $this->errno = 13; 
  93.   // return false; 
  94.   //} 
  95.   // 如果有大小限制,檢查文件是否超過限制 
  96.   if ($this->maxSize != 0 ){ 
  97.    if ($fileArray["size"] > $this->maxSize){ 
  98.     $this->errno = 14; 
  99.     return false; 
  100.    } 
  101.   } 
  102.   // 文件上傳 
  103.   if(!@move_uploaded_file($fileArray["tmp_name"], $this->savePath.$this->saveName)){ 
  104.    $this->errno = $fileArray["error"]; 
  105.    return false; 
  106.   }elseif$this->thumb ){// 創建縮略圖 
  107.    $CreateFunction = "imagecreatefrom".($this->ext == 'jpg' ? 'jpeg' : $this->ext); 
  108.    $SaveFunction = "image".($this->ext == 'jpg' ? 'jpeg' : $this->ext); 
  109.    if (strtolower($CreateFunction) == "imagecreatefromgif"  
  110.     && !function_exists("imagecreatefromgif")) { 
  111.     $this->errno = 16; 
  112.     return false; 
  113.    } elseif (strtolower($CreateFunction) == "imagecreatefromjpeg"  
  114.     && !function_exists("imagecreatefromjpeg")) { 
  115.     $this->errno = 17; 
  116.     return false; 
  117.    } elseif (!function_exists($CreateFunction)) { 
  118.     $this->errno = 18; 
  119.     return false; 
  120.    } 
  121.      
  122.    $Original = @$CreateFunction($this->savePath.$this->saveName); 
  123.    if (!$Original) {$this->errno = 19; return false;} 
  124.    $originalHeight = ImageSY($Original); 
  125.    $originalWidth = ImageSX($Original); 
  126.    $this->returninfo['originalHeight'] = $originalHeight
  127.    $this->returninfo['originalWidth'] = $originalWidth
  128.    /* 
  129.    if (($originalHeight < $this->thumbHeight  
  130.     && $originalWidth < $this->thumbWidth)) { 
  131.     // 如果比期望的縮略圖小,那只Copy 
  132.     move_uploaded_file($this->savePath.$this->saveName,  
  133.      $this->savePath.$this->thumbPrefix.$this->saveName); 
  134.    } else { 
  135.     if( $originalWidth > $this->thumbWidth ){// 寬 > 設定寬度 
  136.      $thumbWidth = $this->thumbWidth ; 
  137.      $thumbHeight = $this->thumbWidth * ( $originalHeight / $originalWidth ); 
  138.      if($thumbHeight > $this->thumbHeight){// 高 > 設定高度 
  139.       $thumbWidth = $this->thumbHeight * ( $thumbWidth / $thumbHeight ); 
  140.       $thumbHeight = $this->thumbHeight ; 
  141.      } 
  142.     }elseif( $originalHeight > $this->thumbHeight ){// 高 > 設定高度 
  143.      $thumbHeight = $this->thumbHeight ; 
  144.      $thumbWidth = $this->thumbHeight * ( $originalWidth / $originalHeight ); 
  145.      if($thumbWidth > $this->thumbWidth){// 寬 > 設定寬度 
  146.       $thumbHeight = $this->thumbWidth * ( $thumbHeight / $thumbWidth ); 
  147.       $thumbWidth = $this->thumbWidth ; 
  148.      } 
  149.     } 
  150.     */ 
  151.     $radio=max(($originalWidth/$this->thumbWidth),($originalHeight/$this->thumbHeight)); 
  152.     $thumbWidth=(int)$originalWidth/$radio
  153.     $thumbHeight=(int)$originalHeight/$radio
  154.     if ($thumbWidth == 0) $thumbWidth = 1; 
  155.     if ($thumbHeight == 0) $thumbHeight = 1; 
  156.     $createdThumb = imagecreatetruecolor($thumbWidth$thumbHeight); 
  157.     if ( !$createdThumb ) {$this->errno = 20; return false;} 
  158.     if ( !imagecopyresampled($createdThumb$Original, 0, 0, 0, 0,  
  159.      $thumbWidth$thumbHeight$originalWidth$originalHeight) ) 
  160.      {$this->errno = 21; return false;} 
  161.     if ( !$SaveFunction($createdThumb,  
  162.      $this->savePath.$this->thumbPrefix.$this->saveName) ) 
  163.      {$this->errno = 22; return false;} 
  164.     
  165.   } 
  166.   // 刪除臨時文件 
  167.   /* 
  168.   if(!@$this->del($fileArray["tmp_name"])){ 
  169.    return false; 
  170.   } 
  171.   */ 
  172.   return true; 
  173.  } 
  174. // 文件格式檢查,MIME檢測 
  175.  function validateFormat(){ 
  176.   if(!is_array($this->fileFormat)  
  177.    || in_array(strtolower($this->ext), $this->fileFormat)  
  178.    || in_array(strtolower($this->returninfo['type']), $this->fileFormat) ) 
  179.    return true; 
  180.   else 
  181.    return false; 
  182.  } 
  183. // 獲取文件擴展名 
  184. // @param $fileName 上傳文件的原文件名 
  185.  function getExt($fileName){ 
  186.   $ext = explode("."$fileName); 
  187.   $ext = $ext[count($ext) - 1]; 
  188.   $this->ext = strtolower($ext); 
  189.  } 
  190. // 設置上傳文件的最大字節限制 
  191. // @param $maxSize 文件大小(bytes) 0:表示無限制 
  192.  function setMaxsize($maxSize){ 
  193.   $this->maxSize = $maxSize
  194.  } 
  195. // 設置文件格式限定 
  196. // @param $fileFormat 文件格式數組 
  197.  function setFileformat($fileFormat){ 
  198.   if(is_array($fileFormat)){$this->fileFormat = $fileFormat ;} 
  199.  } 
  200. // 設置覆蓋模式 
  201. // @param overwrite 覆蓋模式 1:允許覆蓋 0:禁止覆蓋 
  202.  function setOverwrite($overwrite){ 
  203.   $this->overwrite = $overwrite
  204.  } 
  205.  
  206. // 設置保存路徑 
  207. // @param $savePath 文件保存路徑:以 "/" 結尾,若沒有 "/",則補上 
  208.  function setSavepath($savePath){ 
  209.   $this->savePath = substrstr_replace("/","/", $savePath) , -1) == "/"  
  210.   ? $savePath : $savePath."/"
  211.  } 
  212. // 設置縮略圖 
  213. // @param $thumb = 1 產生縮略圖 $thumbWidth,$thumbHeight 是縮略圖的寬和高 
  214.  function setThumb($thumb$thumbWidth = 0,$thumbHeight = 0){ 
  215.   $this->thumb = $thumb
  216.   if($thumbWidth$this->thumbWidth = $thumbWidth
  217.   if($thumbHeight$this->thumbHeight = $thumbHeight
  218.  } 
  219. // 設置文件保存名 
  220. // @param $saveName 保存名,如果為空,則系統自動生成一個隨機的文件名 
  221.  function setSavename($saveName){ 
  222.   if ($saveName == ''){  // 如果未設置文件名,則生成一個隨機文件名 
  223.    $name = date('YmdHis')."_".rand(100,999).'.'.$this->ext; 
  224.    //判斷文件是否存在,不允許重復文件 
  225.    if(file_exists($this->savePath . $name)){ 
  226.     $name = setSavename($saveName); 
  227.     } 
  228.   } else { 
  229.    $name = $saveName
  230.   } 
  231.   $this->saveName = $name
  232.  } 
  233. // 刪除文件 
  234. // @param $fileName 所要刪除的文件名 
  235.  function del($fileName){ 
  236.   if(!@unlink($fileName)){ 
  237.    $this->errno = 15; 
  238.    return false; 
  239.   } 
  240.   return true; 
  241.  } 
  242. // 返回上傳文件的信息 
  243.  function getInfo(){ 
  244.   return $this->returnArray; 
  245.  } 
  246. // 得到錯誤信息 
  247.  function errmsg(){ 
  248.   $uploadClassError = array
  249.    0 =>'There is no error, the file uploaded with success. '
  250.    1 =>'The uploaded file exceeds the upload_max_filesize directive in php.ini.'
  251.    2 =>'The uploaded file exceeds the MAX_FILE_SIZE that was specified in the HTML form.'
  252.    3 =>'The uploaded file was only partially uploaded. '
  253.    4 =>'No file was uploaded. '
  254.    6 =>'Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3. '
  255.    7 =>'Failed to write file to disk. Introduced in PHP 5.1.0. '
  256.    10 =>'Input name is not unavailable!'
  257.    11 =>'The uploaded file is Unallowable!'
  258.    12 =>'Directory unwritable!'
  259.    13 =>'File exist already!'
  260.    14 =>'File is too big!'
  261.    15 =>'Delete file unsuccessfully!'
  262.    16 =>'Your version of PHP does not appear to have GIF thumbnailing support.'
  263.    17 =>'Your version of PHP does not appear to have JPEG thumbnailing support.'
  264.    18 =>'Your version of PHP does not appear to have pictures thumbnailing support.'
  265.    19 =>'An error occurred while attempting to copy the source image .  
  266.      Your version of php ('.phpversion().') may not have this image type support.', 
  267.    20 =>'An error occurred while attempting to create a new image.'
  268.    21 =>'An error occurred while copying the source image to the thumbnail image.'
  269.    22 =>'An error occurred while saving the thumbnail image to the filesystem.  
  270.      Are you sure that PHP has been configured with both read and write access on this folder?', 
  271.    ); 
  272.   if ($this->errno == 0) 
  273.    return false; 
  274.   else 
  275.    return $uploadClassError[$this->errno]; 
  276.  } 
  277. ?> 
  278. 如何使用這個類呢? 
  279. <?php 
  280. //如果收到表單傳來的參數,則進行上傳處理,否則顯示表單 
  281. if(isset($_FILES['uploadinput'])){ 
  282.  //建目錄函數,其中參數$directoryName最后沒有"/", 
  283.  //要是有的話,以'/'打散為數組的時候,最后將會出現一個空值 
  284.  function makeDirectory($directoryName) { 
  285.   $directoryName = str_replace("/","/",$directoryName); 
  286.   $dirNames = explode('/'$directoryName); 
  287.   $total = count($dirNames) ; 
  288.   $temp = ''
  289.   for($i=0; $i<$total$i++) { 
  290.    $temp .= $dirNames[$i].'/'
  291.    if (!is_dir($temp)) { 
  292.     $oldmask = umask(0); 
  293.     if (!mkdir($temp, 0777)) exit("不能建立目錄 $temp");  
  294.     umask($oldmask); 
  295.    } 
  296.   } 
  297.   return true; 
  298.  } 
  299.  if($_FILES['uploadinput']['name'] <> ""){ 
  300.   //包含上傳文件類 
  301.   require_once ('upload_class.php'); 
  302.   //設置文件上傳目錄 
  303.   $savePath = "upload"
  304.   //創建目錄 
  305.   makeDirectory($savePath); 
  306.   //允許的文件類型 
  307.   $fileFormat = array('gif','jpg','jpge','png'); 
  308.   //文件大小限制,單位: Byte,1KB = 1000 Byte 
  309.   //0 表示無限制,但受php.ini中upload_max_filesize設置影響 
  310.   $maxSize = 0; 
  311.   //覆蓋原有文件嗎? 0 不允許  1 允許  
  312.   $overwrite = 0; 
  313.   //初始化上傳類 
  314.   $f = new Upload( $savePath$fileFormat$maxSize$overwrite); 
  315.   //如果想生成縮略圖,則調用成員函數 $f->setThumb(); 
  316.   //參數列表: setThumb($thumb, $thumbWidth = 0,$thumbHeight = 0) 
  317.   //$thumb=1 表示要生成縮略圖,不調用時,其值為 0 
  318.   //$thumbWidth  縮略圖寬,單位是像素(px),留空則使用默認值 130 
  319.   //$thumbHeight 縮略圖高,單位是像素(px),留空則使用默認值 130 
  320.   $f->setThumb(1); 
  321.    
  322.   //參數中的uploadinput是表單中上傳文件輸入框input的名字 
  323.   //后面的0表示不更改文件名,若為1,則由系統生成隨機文件名 
  324.   if (!$f->run('uploadinput',1)){ 
  325.    //通過$f->errmsg()只能得到最后一個出錯的信息, 
  326.    //詳細的信息在$f->getInfo()中可以得到。 
  327.    echo $f->errmsg()."<br>n"
  328.   } 
  329.   //上傳結果保存在數組returnArray中。 
  330.   echo "<pre>"
  331.   print_r($f->getInfo()); 
  332.   echo "</pre>"
  333.  } 
  334. }else
  335. ?> 
  336. <form enctype="multipart/form-data" action="" method="POST"
  337. Send this file: <br /> 
  338. <input name="uploadinput[]" type="file"><br /> 
  339. <input name="uploadinput[]" type="file"><br /> 
  340. <input name="uploadinput[]" type="file"><br /> 
  341. <input type="submit" value="Send File"><br /> 
  342. </form>  
  343. <?php 
  344. ?> 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
日韩精品在线免费播放| 久久91精品国产91久久久| 国产z一区二区三区| 欧美猛男性生活免费| 在线观看视频亚洲| 欧美成人午夜剧场免费观看| 亚洲人精选亚洲人成在线| 国产精品永久免费视频| 2019中文字幕在线| 国产精品久久久久秋霞鲁丝| 狠狠躁夜夜躁人人爽超碰91| 亚洲白虎美女被爆操| 日韩一区二区三区国产| 91精品国产高清久久久久久| 国产色视频一区| 久久夜色精品国产亚洲aⅴ| 91精品久久久久久久久不口人| 91精品91久久久久久| 久久这里只有精品99| 成人欧美在线视频| 久久伊人免费视频| 黑人巨大精品欧美一区免费视频| 亚洲精品国产suv| 亚洲午夜激情免费视频| 国产一区二区三区精品久久久| 亚洲黄色www| 日韩精品免费在线播放| 91九色国产视频| 91中文字幕在线| 91精品久久久久久久久中文字幕| 国产精品wwwwww| 91在线观看免费高清完整版在线观看| 2019亚洲男人天堂| 国产精品福利在线| 色狠狠av一区二区三区香蕉蜜桃| 久久久久久国产精品三级玉女聊斋| 精品亚洲一区二区三区在线观看| 日本精品性网站在线观看| 欧美国产第二页| 在线观看不卡av| 欧美精品成人91久久久久久久| 欧美日韩亚洲一区二区三区| 国产福利精品在线| 亚洲一区av在线播放| 不卡在线观看电视剧完整版| 国产精品羞羞答答| 欧美视频中文在线看| 国产亚洲精品va在线观看| 日韩av成人在线观看| 欧美黄色片在线观看| 欧美性视频精品| 亚洲精品aⅴ中文字幕乱码| 久久久久久久影院| 国产美女高潮久久白浆| 国产精品久久久久久久一区探花| 91在线免费视频| 久久久日本电影| 久久黄色av网站| 国产精品视频免费观看www| 奇米四色中文综合久久| 亚洲一区二区日本| 亚洲春色另类小说| 日本在线观看天堂男亚洲| 91沈先生在线观看| 亚洲a∨日韩av高清在线观看| 日本免费久久高清视频| 欧美一区二区.| 国产热re99久久6国产精品| 日本亚洲欧美成人| 欧美激情精品久久久久久久变态| 黑人巨大精品欧美一区免费视频| 国产精品av在线播放| 亚洲第一色中文字幕| 亚洲成av人影院在线观看| 亚洲精品久久久久国产| 91久久在线播放| 亚洲色图av在线| 国产91在线播放九色快色| 国产日韩欧美在线| 久久色免费在线视频| 亚洲成人1234| 欧美日韩午夜视频在线观看| 国产精品久久久久av| 在线视频日韩精品| 久久久久999| 日韩在线观看成人| 日韩av高清不卡| 亚洲精品久久久久久下一站| 中文综合在线观看| 欧美日韩在线另类| 91黑丝在线观看| 91精品国产色综合久久不卡98口| 国产成人精彩在线视频九色| 日本一区二区在线播放| 性色av一区二区三区| www.久久久久久.com| 欧美xxxx14xxxxx性爽| 日韩av在线直播| 日韩免费精品视频| 国产丝袜一区视频在线观看| 国产精品伦子伦免费视频| 亚洲综合精品伊人久久| 欧美日韩亚洲网| 国产精品女视频| 成人欧美一区二区三区在线湿哒哒| 欧美日韩xxx| 久久精品国产精品亚洲| 欧美性69xxxx肥| 欧美激情第三页| 亚洲天堂成人在线| 国产成人精品综合| 伊人男人综合视频网| 中文字幕综合一区| 亚洲欧美激情视频| 久久天天躁日日躁| 亚洲女人天堂成人av在线| 亚洲欧美在线第一页| 成人动漫网站在线观看| 亚洲午夜小视频| xvideos亚洲人网站| 91av视频在线| 成人h猎奇视频网站| 精品偷拍一区二区三区在线看| 91在线视频成人| 色先锋久久影院av| 亚洲人成电影网站| 欧美综合激情网| 精品一区二区三区电影| 国产精品高清免费在线观看| 亚洲国产精品va在线看黑人| www.亚洲男人天堂| 中文字幕日韩欧美精品在线观看| 国产精品老女人视频| 欧美日韩在线免费| 久久精品国产成人精品| 欧美www视频在线观看| 日韩在线观看免费高清| 国产精品成av人在线视午夜片| 日韩av手机在线看| 91免费电影网站| 亚洲自拍偷拍在线| 久久久91精品国产| 麻豆国产精品va在线观看不卡| 日韩精品在线免费观看视频| 91伊人影院在线播放| 精品久久久久久中文字幕| 久久青草福利网站| 国产精品成人av性教育| 日本欧美爱爱爱| 91国内产香蕉| 日韩av不卡在线| 欧美天堂在线观看| 亚洲第一精品自拍| 富二代精品短视频| …久久精品99久久香蕉国产| 国产日韩中文在线| 91精品国产综合久久香蕉的用户体验| 96精品视频在线| 久久精品国产成人精品| 久久久亚洲精选| 亚洲综合中文字幕在线| 91成人在线视频| 亚洲激情电影中文字幕| 欧美激情精品久久久久久免费印度|