登录  /  注册

php实现上传文件类

墨辰丷
发布: 2018-05-18 10:32:04
原创
953人浏览过

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

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

相关推荐:

php中检测上传文件类型与上传图片大小代码

以上就是php实现上传文件类的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号