溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

php中有哪些常用的文件上傳類

發(fā)布時(shí)間:2021-01-28 11:41:00 來源:億速云 閱讀:201 作者:Leah 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)php中有哪些常用的文件上傳類,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

<?php
/**
 * 上傳文件類
 * @param _path : 服務(wù)器文件存放路徑
 * @param _allowType : 允許上傳的文件類型和所對應(yīng)的MIME
 * @param _file : 上傳的文件信息
 */
class Upload{

 private $_path;
 private $_allowType;
 private $_file;
 /**
  * 構(gòu)造函數(shù)
  * @param string : 服務(wù)器上存放上傳文件的路徑
  */
 function __construct( $path = '' )
 {
  $this->_path = $path;
  $this->_allowType = array(
    // images
    'bmp' => 'image/x-ms-bmp',
    'jpg' => 'image/jpeg',
    'jpeg' => 'image/jpeg',
    'gif' => 'image/gif',
    'png' => 'image/png',
    'tif' => 'image/tiff',
    'tiff' => 'image/tiff',
    'tga' => 'image/x-targa',
    'psd' => 'image/vnd.adobe.photoshop',
    //文本
    'txt' => 'text/plain',
    'php' => 'text/x-php',
    'html' => 'text/html',
    'htm' => 'text/html',
    'js' => 'text/javascript',
    'css' => 'text/css',
    'rtf' => 'text/rtf',
    'rtfd' => 'text/rtfd',
    'py' => 'text/x-python',
    'java' => 'text/x-java-source',
    'rb' => 'text/x-ruby',
    'sh' => 'text/x-shellscript',
    'pl' => 'text/x-perl',
    'sql' => 'text/x-sql',
    //應(yīng)用
    'exe' => 'application/octet-stream',
    'doc' => 'application/vnd.ms-word',
    'docx' => 'application/vnd.ms-word',
    'xls' => 'application/vnd.ms-excel',
    'ppt' => 'application/vnd.ms-powerpoint',
    'pps' => 'application/vnd.ms-powerpoint',
    'pdf' => 'application/pdf',
    'xml' => 'application/xml',
    //音頻
    'mp3' => 'audio/mpeg',
    'mid' => 'audio/midi',
    'ogg' => 'audio/ogg',
    'mp4a' => 'audio/mp4',
    'wav' => 'audio/wav',
    'wma' => 'audio/x-ms-wma',
    //視頻
    'avi' => 'video/x-msvideo',
    'dv' => 'video/x-dv',
    'mp4' => 'video/mp4',
    'mpeg' => 'video/mpeg',
    'mpg' => 'video/mpeg',
    'mov' => 'video/quicktime',
    'wm' => 'video/x-ms-wmv',
    'flv' => 'video/x-flv',
    'mkv' => 'video/x-matroska'
   );
 }
 /**
  * 上傳函數(shù)
  * @param string : 表單元素的name 值
  * @return [type]
  */
 public function upload( $txtName = '' )
 {
  $this->_file = $_FILES[$txtName];
  if( $this->_file['error'] == 0){
   $fileType = end( explode('.', $this->_file['name'] ));
   $allowType = array();
   foreach( $this->_allowType as $item=>$value ){
    $allowType[] = $item;
   }
   if( !in_array($fileType, $allowType)){
    die('上傳的文件格式不正確!');
   }else{
    if(move_uploaded_file($this->file['tmp_name'], ($this->path).$this->file['name']))
     {
      echo "<script>alert('上傳成功!')</script>";
     }
    else
     {
      echo "<script>alert('上傳失敗!');</script>";
     }
   }

  }else{
   //沒有正確上傳
   switch ($this->file['error']){
    case 1:
     die('文件大小超過系統(tǒng)限制。');
     break;
    case 2:
     die('文件大小超過預(yù)定義限制。');
     break;
    case 3:
     die('文件為完全上傳。');
     break;
    case 4:
     die('未上傳任何文件。');
     break;
    default:
     die('上傳出錯');
     break;
   }
  }
 }
 //end upload
}

關(guān)于php中有哪些常用的文件上傳類就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI