PHP的单文件上传类

提交单文件的页面

upload.php

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>文件上传</title>
</head>
<body>
    <form action="doAction.php" method="post" enctype="multipart/form-data">
        <!-- 在客户端可以设置表单MAX_FILE_SIZE限制的大小 -->
        <!--<input type="hidden" name="MAX_FILE_SIZE" value="5*1024*1024">-->
        请选择您要上传的文件:
        <!-- 在客户端设置允许上传的文件类型 --><!-- 但是我们需要记住:客户端所做的任何限制都是不安全的 -->
        <!--<input type="file" name="myFile" accept="image/jpeg,image/gif,image/png"/><br/>-->
        <input type="file" name="myFile2" /><br/>
        <input type="submit" value="上传文件"/>
    </form>
</body>
</html>

单文件上传类 Upload.class.php

<?php
/**
 * Created by PhpStorm.
 * User: DreamBoy
 * Date: 2016/4/9
 * Time: 9:24
 */
error_reporting(0);
class Upload {
    protected $fileName; //POST请求时文件的name值
    protected $maxSize; //文件上传的最大大小
    protected $allowMime; //允许上传的文件类型
    protected $allowExt; //允许上传的文件类型
    protected $uploadPath; //文件上传的路径
    protected $imgFlag; //标志是否要求上传的文件为真实图片
    protected $fileInfo; //上传文件的信息
    protected $ext; //文件扩展名
    protected $uniName; //产生的文件唯一名称(不包含后缀)
    protected $destination; //文件上传的目录

    protected $error; //记录错误号
    protected $err = array( //错误号及错误类型
        '001' => '超过了PHP配置文件中upload_max_filesize选项值',
        '002' => '超过了表单中MAX_FILE_SIZE设置的值',
        '003' => '文件部分被上传',
        '004' => '没有选择上传文件',
        '005' => '没有找到临时目录',
        '006' => '文件不可写',
        '007' => '由于PHP的扩展程序中断文件上传',
        '008' => '上传文件过大',
        '009' => '不允许的文件类型',
        '010' => '不允许的文件MIME类型',
        '011' => '文件不是真实图片',
        '012' => '文件不是通过HTTP POST方式上传上来的',
        '013' => '文件移动失败',
        '014' => '系统错误:文件上传出错',
    );

    /**
     * Upload constructor.
     * @param string $fileName
     * @param string $uploadPath
     * @param bool $imgFlag
     * @param int $maxSize
     * @param array $allowExt
     * @param array $allowMime
     */
    public function __construct($fileName='myFile',$uploadPath='./uploads',$imgFlag=true,$maxSize=5242880,
                                $allowExt=array('jpeg','jpg','png','gif'),
                                $allowMime=array('image/jpeg','image/png','image/gif')) {
        $this->fileName = $fileName;
        $this->maxSize = $maxSize;
        $this->allowMime = $allowMime;
        $this->allowExt = $allowExt;
        $this->uploadPath = $uploadPath;
        $this->imgFlag = $imgFlag;
        $this->fileInfo = $_FILES[$this->fileName];

        $this->ext = strtolower(pathinfo($this->fileInfo['name'], PATHINFO_EXTENSION));
    }

    /**
     * 检测上传文件是否出错
     * @return bool
     */
    protected function checkError() {
        if(is_null($this->fileInfo)) { //文件获取失败
            $this->error = '014';
            return false;
        }

        if($this->fileInfo['error']>0) {
            switch($this->fileInfo['error']) {
                case 1:
                    $this->error = '001';
                    break;
                case 2:
                    $this->error = '002';
                    break;
                case 3:
                    $this->error = '003';
                    break;
                case 4:
                    $this->error = '004';
                    break;
                case 6:
                    $this->error = '005';
                    break;
                case 7:
                    $this->error = '006';
                    break;
                case 8:
                    $this->error = '007';
                    break;
            }
            return false;
        }
        return true;
    }

    /**
     * 检测上传文件的大小
     * @return bool
     */
    protected function checkSize() {
        if($this->fileInfo['size'] > $this->maxSize) {
            $this->error = '008';
            return false;
        }
        return true;
    }

    /**
     * 检测文件扩展名
     * @return bool
     */
    protected function checkExt() {
        if(!in_array($this->ext, $this->allowExt)) {
            $this->error = '009';
            return false;
        }
        return true;
    }

    /**
     * 检测文件的MIME类型
     * @return bool
     */
    protected function checkMime() {
        if(!in_array($this->fileInfo['type'],$this->allowMime)) {
            $this->error = '010';
            return false;
        }
        return true;
    }

    /**
     * 检测文件是否为真实图片
     * @return bool
     */
    protected function checkTrueImg() {
        if($this->imgFlag) {
            if(!@getimagesize($this->fileInfo['tmp_name'])) {
                $this->error = '011';
                return false;
            }
        }
        return true;
    }

    /**
     * 检测是否通过HTTP Post方式上传过来的
     * @return bool
     */
    protected function checkHTTPPost() {
        if(!is_uploaded_file($this->fileInfo['tmp_name'])) {
            $this->error = '012';
            return false;
        }
        return true;
    }

    /**
     * 显示错误
     */
    protected function showError() {
        $e = $this->err[$this->error];
        exit('<span style="color:red">' . $e . '</span>');
    }

    /**
     * 检测目录是否存在,如果不存在则进行创建
     */
    protected function checkUploadPath() {
        if(!file_exists($this->uploadPath)) {
            mkdir($this->uploadPath, 0777, true);
        }
    }

    /**
     * 产生唯一字符串
     * @return string
     */
    protected function getUniName() {
        return md5(uniqid(microtime(true),true));
    }

    /**
     * 上传文件
     * @return string
     */
    public function uploadFile() {
        if($this->checkError() && $this->checkSize()
            && $this->checkExt() && $this->checkMime()
            && $this->checkTrueImg() && $this->checkHTTPPost()) {

            $this->checkUploadPath();

            $this->uniName = $this->getUniName();
            $this->destination = $this->uploadPath . '/' . $this->uniName . '.' . $this->ext;
            if(@move_uploaded_file($this->fileInfo['tmp_name'], $this->destination)) {
                return $this->destination;
            } else {
                $this->error = '013';
                $this->showError();
            }
        } else {
            $this->showError();
        }
    }
}


doAction.php

<?php
/**
 * Created by PhpStorm.
 * User: DreamBoy
 * Date: 2016/4/9
 * Time: 10:31
 */
header('content-type:text/html;charset=utf-8');
require_once 'Upload.class.php';
//$upload = new Upload();
//$upload = new Upload('myFile2');
$upload = new Upload('myFile2','uploads2');
$dest = $upload->uploadFile();
echo $dest;


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
文件是网站开发中常用的功能之一,PHP文件类可以帮助我们实现文件功能。当用户需要上文件时,可以使用PHP文件类来处理文件的逻辑。PHP文件类可以实现对上文件的各种验证,例如文件类型、大小、保存路径等。通过使用PHP文件类,可以让文件变得更加安全、简和高效。 PHP文件类可以通过封装上文件的函数来实现文件的上和下载。上文件的功能通过对文件的验证和处理,将文件保存到服务器指定的位置。而下载文件的功能通过设置文件的下载头信息,实现对指定文件的下载操作。PHP文件类还可以处理文件重名、文件大小限制、文件类型限制等问题,保证文件过程中的安全性和完整性。 在使用PHP文件类的过程中,需要注意对上文件的安全性进行严格的检查,避免出现恶意文件或者文件被非法下载的情况。同时,需要确保服务器环境对文件和下载的操作进行了正确的配置,以确保文件和下载功能的正常运行。 总的来说,PHP文件类可以极大地方便我们在网站开发中对文件的上和下载操作,帮助我们完成文件处理的各种功能。无论是图片、文档还是音视频文件PHP文件类都可以帮助我们完成文件和下载功能,为网站的用户提供更好的使用体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值