實例講解php實作常用文件上傳類

巴扎黑
發布: 2023-03-15 19:48:01
原創
1452 人瀏覽過

下面小編就為大家帶來一篇php實作常用檔案上傳類別的範例。小編覺得蠻不錯的,現在就分享給大家,也給大家做個參考。一起跟著小編過來看看吧

廢話不多說,直接上程式碼:


<?php
/**
 * 上传文件类
 * @param _path : 服务器文件存放路径
 * @param _allowType : 允许上传的文件类型和所对应的MIME
 * @param _file : 上传的文件信息
 */
class Upload{

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

  }else{
   //没有正确上传
   switch ($this->file[&#39;error&#39;]){
    case 1:
     die(&#39;文件大小超过系统限制。&#39;);
     break;
    case 2:
     die(&#39;文件大小超过预定义限制。&#39;);
     break;
    case 3:
     die(&#39;文件为完全上传。&#39;);
     break;
    case 4:
     die(&#39;未上传任何文件。&#39;);
     break;
    default:
     die(&#39;上传出错&#39;);
     break;
   }
  }
 }
 //end upload
}
登入後複製

以上是實例講解php實作常用文件上傳類的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!