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

PHP文件上传类

时间:2016-05-30 10:03:21      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:

<?php

class Upload {
    private $error;
    private $destination;
    private $fileInfo;
    private $uploadDir;
    private $maxFileSize;
    private $allowExt;
    private $checkImage;

    function __construct($fileInfo, $uploadDir = ‘./uploads‘, $maxFileSize = 1024 * 1024 * 1,
                         $allowExt = array(‘jpg‘, ‘gif‘, ‘png‘, ‘bmp‘, ‘jpeg‘),
                         $checkImage = true, $allowMime = array(‘image/gif‘, ‘image/jpeg‘, ‘image/png‘)) {
        $this->fileInfo = $fileInfo;
        $this->uploadDir = $uploadDir;
        $this->maxFileSize = $maxFileSize;
        $this->allowExt = $allowExt;
        $this->checkImage = $checkImage;
        $this->allowMime = $allowMime;
    }

    /***
     * 处理上传文件
     * @return string
     */
    public function uploadFile() {
        if (!$this->fileInfo) {
            $this->error = ‘文件信息不存在‘;
            return $this->error;
        }

        if ($this->checkError() && $this->checkImageType() && $this->checkMaxFileSize() && $this->checkAllowExt()
            && $this->checkPostUpload() && $this->checkMime()
        ) {
            //判断文件夹是否存在
            $this->checkUploadDir();
            //生成唯一文件名
            $destination = $this->uploadDir . ‘/‘ . $this->buildUniqueId() . ‘.‘ . $this->getExt();
            if (@move_uploaded_file($this->fileInfo[‘tmp_name‘], $destination)) {
                $this->destination = $destination;
                return $this->destination;
            } else {
                $this->error = ‘移动上传文件失败‘;
            }
            return $this->error;
        }

        return $this->error;
    }

    private function checkError() {
        if ($this->fileInfo[‘error‘] == UPLOAD_ERR_OK) {
            return true;
        }
        $this->error = ‘上传出现错误,错误代码:‘ . $this->fileInfo[‘error‘];
        return false;
    }

    private function buildUniqueId() {
        return md5(uniqid(microtime(true), true));
    }

    private function checkUploadDir() {
        if (!file_exists($this->uploadDir)) {
            mkdir($this->uploadDir, 0777, true);
            chmod($this->uploadDir, 0777);
        }
    }

    private function checkAllowExt() {
        if (in_array($this->getExt(), $this->allowExt)) {
            return true;
        }
        $this->error = ‘该类型的文件不能上传‘;
        return false;
    }

    private function checkMaxFileSize() {
        if ($this->maxFileSize > $this->fileInfo[‘size‘]) {
            return true;
        }
        $this->error = ‘上传文件的大小已超过限额‘;
        return false;
    }

    private function checkImageType() {
        if ($this->checkImage) {
            if ($this->checkAllowExt() && getimagesize($this->fileInfo[‘tmp_name‘])) {
                return true;
            }
            $this->error = ‘上传的不是图片‘;
            return false;
        }
        return true;
    }

    private function checkPostUpload() {
        if (is_uploaded_file($this->fileInfo[‘tmp_name‘])) {
            return true;
        }
        $this->error = ‘不是Post上传的文件‘;
        return false;
    }

    private function checkMime() {
        if (in_array($this->getMime(), $this->allowMime)) {
            return true;
        }
        $this->error = ‘Mime类型不正确‘;
        return false;
    }

    private function getMime() {
        if ($imageInfo = getimagesize($this->fileInfo[‘tmp_name‘])) {
            return $imageInfo[‘mime‘];
        }
        return false;
    }

    private function getExt() {
        return strtolower(pathinfo($this->fileInfo[‘name‘], PATHINFO_EXTENSION));
    }
}

  

PHP文件上传类

标签:

原文地址:http://www.cnblogs.com/99388514/p/5541271.html

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