php文件上传类完整实例


Posted in PHP onMay 14, 2016

本文实例讲述了php文件上传类。分享给大家供大家参考,具体如下:

/**
$file=new class_file($file_array,"flash/");
 $file->set_allow_type(array("jpg","jpeg","gif"));
 $file->is_limit_size();
 if(!$file->allow_file_size()){
   echo $file->error;
   exit;
 }
 if(!$file->allow_file_type()){
    echo $file->error;
    exit();
  }else if(!$file->uploadfile()){
  echo $file->error;
  exit;
 }
**/
<?php
 class class_file{
   private $file_type;
   private $file_size;
   private $save_path;
   private $file_path;
   private $allow_type=array();
   private $allow_size;
   private $file_name;
   private $flag=false;
   private $mime_type;
   private $is_limit_size=false;
   public $error;
   //构造函数
  function class_file($file_array,$save_path){
       $this->file_path=$file_array['tmp_name'];
       $this->file_size=$file_array['size'];
       $this->file_type=$file_array['type'];
       $this->save_path=$save_path;
    }
    //设置允许的文件类型
   function set_allow_type($allow_type){
      $this->allow_type=$allow_type;
    }
    //设置允许的文件大小
   function set_allow_size($allow_size){
      $this->allow_size=$allow_size;
    }
    //文件上传
   public function uploadfile(){
     if(!$this->allow_file_type()){
     $this->file_name();
     }
     if(move_uploaded_file($this->file_path,$this->save_path.$this->file_name)){
       return true;
     }else{
       $this->error="文件上传失败";
       return;
     }
   }
//判断文件上传的类型
   function allow_file_type(){
     $this->file_name();
     if(in_array($this->mime_type,$this->allow_type)){
         return true;
       }else{
         $this->error="不允许上传的类型";
         exit();
       }
   }
 //判断文件上传的大小
   function allow_file_size($size=100){
     if($this->is_limit_size){
     $this->set_allow_size($size);
     if($this->allow_size>=$this->file_size){
       return true;
     }else{
       $this->error="超过文件上传大小限制";
     }
     }
   }
 //是否限制文件大小
   function is_limit_size(){
     $this->is_limit_size=true;
   }
//文件类型和文件名称
   function file_name(){
    $this->mime_type=substr($this->file_type,strpos($this->file_type,"/")+1);
   if($this->mime_type=="pjpeg"){
     $this->mime_type="jpg";
    }
   if($this->mime_type=="x-ms-wma"){
      $this->mime_type="wma";
    }
    if($this->mime_type=="x-ms-wmv"){
      $this->mime_type="wmv";
    }
    $this->file_name=date("YmdHis").".$this->mime_type";
   }
   function _get_file_name(){
     return $this->file_name;
   }
 }
?>

希望本文所述对大家PHP程序设计有所帮助。

PHP 相关文章推荐
利用递归把多维数组转为一维数组的函数
Oct 09 PHP
默默小谈PHP&amp;MYSQL分页原理及实现
Jan 02 PHP
PHP的可变变量名的使用方法分享
Feb 05 PHP
CI框架给视图添加动态数据
Dec 01 PHP
php使用MySQL保存session会话的方法
Jun 18 PHP
PHP易混淆知识整理笔记
Sep 24 PHP
php pthreads多线程的安装与使用
Jan 19 PHP
php基于curl实现随机ip地址抓取内容的方法
Oct 11 PHP
php实现文章置顶功能的方法
Oct 20 PHP
PHP四种排序算法实现及效率分析【冒泡排序,插入排序,选择排序和快速排序】
Apr 27 PHP
PHP实现15位身份证号转18位的方法分析
Oct 16 PHP
thinkphp5.1 框架钩子和行为用法实例分析
May 25 PHP
Smarty高级应用之缓存操作技巧分析
May 14 #PHP
php生成Android客户端扫描可登录的二维码
May 13 #PHP
php短信接口代码
May 13 #PHP
php实现在线通讯录功能(附源码)
May 13 #PHP
PHP将MySQL的查询结果转换为数组并用where拼接的示例
May 13 #PHP
php仿微信红包分配算法的实现方法
May 13 #PHP
PHP简单实现无限级分类的方法
May 13 #PHP
You might like
PHP中在数据库中保存Checkbox数据(1)
2006/10/09 PHP
聊天室php&amp;mysql(一)
2006/10/09 PHP
php处理斐波那契数列非递归方法
2012/02/04 PHP
php页面防重复提交方法总结
2013/11/25 PHP
php实现base64图片上传方式实例代码
2017/02/22 PHP
PHP SFTP实现上传下载功能
2017/07/26 PHP
PHP unset函数原理及使用方法解析
2020/08/14 PHP
网页里控制图片大小的相关代码
2006/06/25 Javascript
jquery EasyUI的formatter格式化函数代码
2011/01/12 Javascript
JQuery判断子iframe何时加载完成解决方案
2013/08/20 Javascript
Jquery uploadify图片上传插件无法上传的解决方法
2013/12/16 Javascript
jQuery中操控hidden、disable等无值属性的方法
2014/01/06 Javascript
js实现遮罩层划出效果是生成div而不是显示
2014/07/29 Javascript
jQuery实现图片轮播特效代码分享
2015/09/15 Javascript
详解JS中统计函数执行次数与执行时间
2018/09/04 Javascript
详解如何理解vue的key属性
2019/04/14 Javascript
layer页面跳转,获取html子节点元素的值方法
2019/09/27 Javascript
[01:05:41]EG vs Optic Supermajor 败者组 BO3 第二场 6.6
2018/06/07 DOTA
[58:00]DOTA2-DPC中国联赛 正赛 PSG.LGD vs Elephant BO3 第二场 2月7日
2021/03/11 DOTA
Python3下错误AttributeError: ‘dict’ object has no attribute’iteritems‘的分析与解决
2017/07/06 Python
详解python OpenCV学习笔记之直方图均衡化
2018/02/08 Python
浅谈tensorflow1.0 池化层(pooling)和全连接层(dense)
2018/04/27 Python
Python学习笔记之抓取某只基金历史净值数据实战案例
2019/06/03 Python
python提取照片坐标信息的实例代码
2019/08/14 Python
Python基于requests实现模拟上传文件
2020/04/21 Python
在TensorFlow中实现矩阵维度扩展
2020/05/22 Python
python能否java成为主流语言吗
2020/06/22 Python
Python绘图之柱形图绘制详解
2020/07/28 Python
Python 微信公众号文章爬取的示例代码
2020/11/30 Python
CSS书写规范、顺序和命名规则
2014/03/06 HTML / CSS
就业推荐自我鉴定
2013/10/06 职场文书
幼儿园秋游感想
2014/03/12 职场文书
法制宣传日活动总结
2014/04/29 职场文书
建设投标担保书
2014/05/13 职场文书
大学生入党群众意见书
2015/06/02 职场文书
Python OpenCV 图像平移的实现示例
2021/06/04 Python