zoukankan      html  css  js  c++  java
  • 关于上传图片的类(有点粗糙)

    <?php
    class Upload
    {
        private $doc;            //文件
        private $docsize;        //文件大小
        private $docname;        //文件名字
        private $doctype;        //文件类型
        public $docnewname;        //图片新名字
        private $allowtype;        //可以上传的文件类型
        private $seterror=1;    //错误原因
        private $doctmpname;    //文件临时名
        private $path;            //文件存储路径
        function __construct($file,$path)
        {
            $this->doc=$file;
            $this->path=$path;
            $this->docsize=$file['size'];
            $this->docname=$file['name'];
            $this->doctmpname=$file['tmp_name'];
            $array=explode('.',$this->docname);
            $allowtype=Array("jpg","jpeg","gif","png","bmp");
            $this->doctype=$array[count($array)-1];
            $this->docnewname=time().".".$this->doctype;
            $error=$file['error'];
            if($error!=0)
            {
                switch($error)
                {
                    case 1:$this->seterror="上传的文件过大,最大能上传2M";break;
                    case 2:$this->seterror="上传的文件过大,最大能上传2M";break;
                    case 3:$this->seterror="文件只有部分被上传";break;
                    case 4:$this->seterror="文件没有被上传";break;
                    case 6:$this->seterror="找不到临时文件夹";break;
                    case 7:$this->seterror="文件写入失败";break;
                }
            }
            else
            {
                if(!in_array($this->doctype,$allowtype))
                {
                    $this->seterror="文件类型不符";
                }
                else
                {
                    if(!is_uploaded_file($this->doctmpname))
                    {
                        $this->seterror="非法操作";
                    }
                    else
                    {
                        if(!file_exists($this->path))
                        {
                            mkdir($this->path,07777);
                        }
                        else
                        {
                            if(!move_uploaded_file($this->doctmpname,$this->path.$this->docnewname))
                            {
                                $this->seterror="文件上传失败";
                            }
                        }
                    }
                }
            }
        }
        function get_img_newname()
        {
            if($this->seterror!=1)
            {
                echo $this->seterror;exit;
            }
            else
            {
                return $this->path.$this->docnewname;
            }
        }
    }

    ?>

  • 相关阅读:
    古典兔子问题
    (I/O流)在100ms内桌面上生成一个200M大小的文件
    搭建手机UI自动化
    关于String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    关于数据库范式的理解
    Orcl分页查询的语法示例
    Eclipse alt+/语法不提示的解决方法
    redis 使用rdb从高版本迁移至低版本
    redis集群详解
    Linux firewall防火墙设置
  • 原文地址:https://www.cnblogs.com/S-Ping/p/4095844.html
Copyright © 2011-2022 走看看