首页 > 后端开发 > php教程 > 实例讲解php实现常用文件上传类

实例讲解php实现常用文件上传类

巴扎黑
发布: 2023-03-15 19:48:01
原创
1503 人浏览过

下面小编就为大家带来一篇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
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板