码迷,mamicode.com
首页 > Web开发 > 详细

<小田吃饺子> PHP:图片上传

时间:2017-09-25 18:57:30      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:attr   time   保存   后缀名   unset   file   upload   for   null   

<?php
class TL_Update_File{
    private $file = null;//接受图片名称
    private $files = [];//图片属性
    public $size = null;//上传文件大小限制
    public $save_path = null;//保存路径
    public $mime = null;//文件上传支持类型
    private $error = null;//图片不符合要求者
    //接受图片
    function __construct($file=null){
        $this->save_path = str_replace("\\","/",dirname(__FILE__))."/";
        $this->file = $file;
        if($file){
            $this->files = $_FILES[$file];
        }else{
            $this->files = $_FILES;
        }
    }
    //一系列属性向
    private function attribute(){
        if($this->file){
            $this->empty_ture();
        }else{
            $this->empty_false();
        }
        if($this->size){
            if($this->file){
                $this->Size_ture();
            }else{
                $this->Size_false();
            }
        }
        if($this->mime){
            if($this->file){
                $this->mime_ture();
            }else{
                $this->mime_false();
            }
        }
    }
    //判断图片是否为空
    function empty_false(){
        foreach ($this->files as $key => $value){
            if($this->files[$key][‘error‘]){
                $this->error[$key] = $value;
                $this->error[$key][‘tl_error‘] = "改图片为空";
                unset($this->files[$key]);
            }else{
                $this->files[$key][‘mime‘] = substr($value[‘name‘], strrpos($value[‘name‘], ".")+1);
            }
        }
    }
    //判断图片是否为空
    function empty_ture(){
        if($this->files[‘error‘]) {
            $this->error[$this->file] = $this->files;
            $this->error[$this->file][‘tl_error‘] = "改图片为空";
            $this->files=[];
        }else{
            $this->files[‘mime‘] = substr($this->files[‘name‘], strrpos($this->files[‘name‘], ".") + 1);
        }
    }
    //文件后缀名是否受限制
    function mime_ture(){
        if($this->files){
            if(!in_array($this->files[‘mime‘],$this->mime)){
                $this->error[$this->file] = $this->files;
                $this->error[$this->file][‘tl_error‘] = "不支持改后缀名";
                $this->files=[];
            }
        }
    }
    //文件后缀名是否受限制
    function mime_false(){
        foreach ($this->files as $key => $value){
            if(!in_array($this->files[$key][‘mime‘],$this->mime)){
                $this->error[$key] = $value;
                $this->error[$key][‘tl_error‘] = "不支持改后缀名";
                unset($this->files[$key]);
            }
        }
    }
    //文件大小是否受限制
    function Size_ture(){
        if($this->files[‘size‘] > $this->size){
            $this->error[$this->file] = $this->files;
            $this->error[$this->file] = $this->files;
            $this->error[$this->file][‘tl_error‘] = "文件过大";
            $this->files=[];
        }
    }
    //文件大小是否受限制
    function Size_false(){
        foreach ($this->files as $key => $value){
            if($this->files[$key][‘size‘] > $this->size){
                $this->error[$key] = $value;
                $this->error[$key][‘tl_error‘] = "文件过大";
                unset($this->files[$key]);
            }
        }
    }
    //保存图片
    function save_file(){
        if($this->files){
            $file_path_dir = date(‘Ym‘,time()) ."/";
            $file_name = time() . rand(00000,99999) . "." . $this->files["mime"];
            $this->is_dir_on_off($this->save_path . date(‘Ym‘,time()) ."/");
            if($this->file){
                $this->save_file_ture($file_path_dir,$file_name);
            }else{
                $this->save_file_false($file_path_dir,$file_name);
            }
        }
    }
    //保存图片
    function save_file_ture($file_path_dir,$file_name){
        move_uploaded_file($this->files["tmp_name"] , $this->save_path . $file_path_dir . $file_name);
    }
    //保存图片
    function save_file_false($file_path_dir,$file_name){
        foreach ($this->files as $key => $value){
            move_uploaded_file($this->files[$key]["tmp_name"] , $this->save_path . $file_path_dir . $file_name);
        }
    }
    //创建目录
    function is_dir_on_off($file_path){
        if(!is_dir($file_path)){
            mkdir($file_path,0777);
        }
    }
    //保存文件
    function move(){
        $this->attribute();
        $this->save_file();
    }
}

  

<小田吃饺子> PHP:图片上传

标签:attr   time   保存   后缀名   unset   file   upload   for   null   

原文地址:http://www.cnblogs.com/hello-tl/p/7593033.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!