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

    前言
      在php开发中,必不可少要用到文件上传,整理封装了一个图片上传的类也很有必要。
      图片上传的流程图


    一、控制器调用
    public function upload_file() {
        if (IS_POST) {
            if (!empty($_FILES['Filedata'])) {
                import('Org.Upload', COMMON_PATH);
                $upload = new Upload();
                // 允许上传文件大小
                $upload->allowMaxSize(C('ALLOW_UPLOAD_FILE_MAX_SIZE'));
                // 允许上传的文件类型
                $upload->allowExt(C('ALLOW_UPLOAD_FILE_TYPE'));
                // 获取上传文件的信息
                $upload->get_upload_file_info($_FILES['Filedata']);
                // 指定上传目录
                $upload->root_dir(ROOT_PATH);
                // 生成文件名
                $file_name = $upload->upload_file_name();
                // 保存到指定的目录
                $res = $upload->save_file('Uploads/', $file_name);
                if ($res === false) {
                    dump($upload->get_error());
                } else {
                    echo '上传成功';
                }
            }
        }
    }
    

      


    二、文件上传类代码
    <?php
    
    /**
     * @desc 文件上传类
     * @author Timothy
     * Created by PhpStorm.
     * Date: 2016/10/17
     * Time: 12:18
     */
    class Upload
    {
        protected $_error = '';
        protected $_allowExt = array();
        protected $_allowSize = 0;
        protected $file_info = null;
        protected $_root_dir = null;
    
        /**
         * @desc 上传文件信息
         * @param array $file_info
         * @return bool
         */
        public function get_upload_file_info(array $file_info = array()) {
            // 判断错误号是否>0
            if ($file_info['error'] > 0) {
                $this->_checkError($file_info['error']);
            }
    
            // 判断是否是通过HTTP POST方式上传
            if (!is_uploaded_file($file_info['tmp_name'])) {
                $this->_error = '文件不是通过HTTP POST方式上传的';
                return false;
            }
    
            $this->file_info = $this->_format_upload_file_info($file_info);
        }
    
        /**
         * @desc 格式化上传文件的信息
         * @param array $file_info
         * @return array
         */
        private function _format_upload_file_info(array $file_info = array()) {
            $pathinfo = pathinfo($file_info['name'], PATHINFO_EXTENSION);
            $file_info['extension'] = $pathinfo;
            return $file_info;
        }
    
        // 上传文件的错误信息处理
        private function _checkError($file_error = '') {
            switch ($file_error) {
                case UPLOAD_ERR_INI_SIZE:
                    $this->_error = '上传文件超过了PHP配置文件中upload_max_filesize选择项的值';
                    return false;
                    break;
                case UPLOAD_ERR_FORM_SIZE:
                    $this->_error = '超过了表单MAX_FILE_SIZE限制的大小';
                    return false;
                    break;
                case UPLOAD_ERR_PARTIAL:
                    $this->_error = '文件部分被上传';
                    return false;
                    break;
                case UPLOAD_ERR_NO_FILE:
                    $this->_error = '没有文件被上传';
                    return false;
                    break;
                case UPLOAD_ERR_NO_TMP_DIR :
                    $this->_error = '找不到临时文件';
                    return false;
                    break;
                case UPLOAD_ERR_CANT_WRITE:
                    $this->_error = '文件写入失败';
                    return false;
                    break;
                case UPLOAD_ERR_EXTENSION:
                    $this->_error = '文件类型不正确';
                    return false;
                    break;
                default:
                    $this->_error = '系统错误';
                    return false;
                    break;
            }
        }
    
        /**
         * @desc 判断上传文件的类型
         * @param string $file_type
         * @return bool
         */
        private function _checkExt($file_type = '') {
            if (!in_array($file_type, $this->_allowExt)) {
                $this->_error = '上传的文件类型不正确';
                return false;
            }
            return true;
        }
    
        /**
         * @desc 判断上传文件的大小
         * @param int $file_size
         * @return bool
         */
        private function _checkSize($file_size = 0) {
            if ($file_size > $this->_allowSize) {
                $this->_error = '上传的图片过大';
                return false;
            }
            return true;
        }
    
        /**
         * @desc 处理允许上传的文件类型
         * @param int $max_size
         * @return bool|int
         */
        public function allowMaxSize($max_size = 2) {
            if (!is_numeric($max_size)) {
                $this->_error = '允许上传的文件大小不正确';
                return false;
            }
            $this->_allowSize = $max_size * 1024 * 1024;
        }
    
        /**
         * @desc 处理允许上传的文件类型
         * @param string $ext
         */
        public function allowExt($ext = '') {
            $this->_allowExt = explode('|', $ext);
        }
    
        /**
         * @desc 指定路径
         * @param none
         * @return void
         */
        public function root_dir($dir) {
            $this->_root_dir = $dir;
        }
    
        /**
         * @desc 递归创建目录
         * @param $path
         * @return bool
         */
        private function _mkdir($path) {
            if (!is_dir($path)) {
                if (@mkdir($path, 0777, true))
                    return true;
                else
                    $this->_error = '目录创建失败';
                return false;
            } else {
                return true;
            }
        }
    
        // 生成一个唯一的文件名,防止因重名而被覆盖
        public function upload_file_name() {
            return md5(uniqid(microtime(true), true)) . '.' . $this->file_info['extension'];
        }
    
        /**
         * @desc 把上传的临时文件保存到指定目录
         * @param string $path
         * @param string $file_name
         * @return string
         */
        public function save_file($path = '', $file_name = '') {
            // 判断是否是合法的文件类型
            if (!$this->_checkExt($this->file_info['extension'])) {
                return false;
            }
    
            // 判断是否是合法的文件大小
            if (!$this->_checkSize($this->file_info['size'])) {
                return false;
            }
    
            if (!$this->_checkTrueImage($this->file_info['tmp_name'])) {
                return false;
            }
    
            $abs_path = $this->_root_dir ? $this->_root_dir . $path  : $path ;
            if ($this->_mkdir($abs_path)) {
                if (move_uploaded_file($this->file_info['tmp_name'], $abs_path . $file_name)) {
                    @chmod($abs_path, 0666);
                    return $abs_path;
                }
            } else {
                $this->_error = '上传文件失败';
            }
        }
    
        /**
         * @desc 判断是否是真实的图片
         * @param string $file_info
         * @return bool
         */
        private function _checkTrueImage($file_info = '') {
            if (!getimagesize($file_info)) {
                $this->_error = '文件不是真实的图片';
                return false;
            }
            return true;
        }
    
        /**
         * @desc 获取上传错误信息,然后返回
         * @return string
         */
        public function get_error() {
            return $this->_error;
        }
    }
    

      

    
    
  • 相关阅读:
    基于MATLAB求解矩阵的正交补矩阵
    MySQL的安装与配置
    删除ubuntu后修复win7系统的引导
    VS2010中快捷添加命名空间
    java学习之函数
    java学习之break 和 continue
    For循环复杂练习
    For循环练习之99乘法表和转义字符
    java学习之语句结构
    java学习之运算符
  • 原文地址:https://www.cnblogs.com/timothy-lai/p/5971620.html
Copyright © 2011-2022 走看看