zoukankan      html  css  js  c++  java
  • PHP:图片上传

    文章来源:http://www.cnblogs.com/hello-tl/p/7593033.html 

    <?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();
        }
    }
    

    文章来源:http://www.cnblogs.com/hello-tl/p/7593033.html  

  • 相关阅读:
    maven打包时出现 Failed to execute goal org.apache.maven.plugins:maven-install-plugin:2.4:install (default-install) on project……
    maven+springmvc出现:java.sql.SQLException: Unknown system variable 'query_cache_size'
    IDEA复制某个类的包名路径
    IDEA中更改Tomcat服务器的URL
    IDEA去除自动检测bean是否存在
    ASM ClassReader failed to parse class file
    The content of element type "web-app" must match "(icon?,display-name?,description?,distributable?,context-param*,filter*,filter-mapping*,listener*,servlet*,servlet- mapping*,session-config?,mime-map
    Could not open ServletContext resource [/WEB-INF/xxx-servlet.xml]
    Eclipse+maven 构建第一个简单的springmvc项目
    Eclispe中编辑xml配置文件时不会提示也不能自动调整格式
  • 原文地址:https://www.cnblogs.com/hello-tl/p/7593033.html
Copyright © 2011-2022 走看看