<?php
$up = new Upload();
$newPath = $up->uploadFile('fm');
if ($newPath === false) {
var_dump($up->errorNumber);
var_dump($up->errorInfo);
} else {
echo $newPath;
} class Upload
{
//文件上传保存路径
protected $path = './upload/';
//允许文件上传的后缀
protected $allowSuffix = ['jpg', 'jpeg', 'gif', 'wbmp', 'png'];
//允许文件上传的Mime类型
protected $allowMime = ['image/jpeg', 'image/gif', 'image/wbmp', 'image/png'];
//允许文件上传的文件最大大小
protected $maxSize = 2000000;
//是否启用随机名
protected $isRandName = true;
//加上文件前缀
protected $prefix = 'up_'; //自定义的错误号码和错误信息
protected $errorNumber;
protected $errorInfo; //文件的信息
protected $oldName; //文件名
protected $suffix; //文件后缀
protected $size; //文件大小
protected $mime; //文件mime
protected $tmpName; //文件临时路径
protected $newName; //文件新名字 public function __construct($arr = [])
{
foreach ($arr as $key => $value) {
$this->setOption($key, $value);
}
} public function __get($name)
{
if ($name == 'errorNumber') {
return $this->errorNumber;
} else if ($name == 'errorInfo') {
return $this->getErrorInfo();
}
} /**
* 判断这个$key是不是我的成员属性,如果是,则设置
*
* @param [type] $key
* @param [type] $value
* @return void
*/
protected function setOption($key, $value)
{
//得到所有的成员属性
$keys = array_keys(get_class_vars(__CLASS__));
if (in_array($key, $keys)) {
$this->$key = $value;
}
} /**
* 文件上传函数
* $key 就是你input框中的name属性值
*
* @param [type] $key
* @return void
*/
public function uploadFile($key)
{
//判断有没有设置路径 path
if (empty($this->path)) {
$this->setOption('errorNumber', -1);
return false;
}
//判断该路径是否存在,是否可写
if (!$this->check()) {
$this->setOption('errorNumber', -2);
return false;
}
//判断$_FILES里面的error信息是否为0,如果为0,说明文件信息在服务器端可以直接获取,提取信息保存到成员属性中
$error = $_FILES[$key]['error'];
if ($error) {
$this->setOption('errorNumber', $error);
return false;
} else {
//提取文件相关信息并且保存到成员属性中
$this->getFileInfo($key);
}
//判断文件的大小、mime、后缀是否符合
if (!$this->checkSize() || !$this->checkMime() || !$this->checkSuffix()) {
return false;
}
//得到新的文件名字
$this->newName = $this->createNewName();
//判断是否是上传文件,并且移动上传文件
if (is_uploaded_file($this->tmpName)) {
if (move_uploaded_file($this->tmpName, $this->path . $this->newName)) {
return $this->path . $this->newName;
} else {
$this->setOption('errorNumber', -7);
return false;
}
} else {
$this->setOption('errorNumber', -6);
return false;
}
} /**
* 得到文件的新名字
*
* @return void
*/
protected function createNewName()
{
//判断是否使用随机名
if ($this->isRandName) {
$name = $this->prefix . uniqid() . '.' . $this->suffix;
} else {
$name = $this->prefix . $this->oldName;
}
return $name;
} /**
* 判断该路径是否存在,是否可写
*
* @return void
*/
protected function check()
{
//文件夹不存在或者不是目录。创建文件夹
if (!file_exists($this->path) || !is_dir($this->path)) {
return mkdir($this->path, 0777, true);
}
//判断文件是否可写
if (!is_writeable($this->path)) {
return chmod($this->path, 0777);
}
return true;
} /**
* 提取文件相关信息并且保存到成员属性中
*
* @param [type] $key
* @return void
*/
protected function getFileInfo($key)
{
// 得到文件名字
$this->oldName = $_FILES[$key]['name'];
//得到文件的mime类型
$this->mime = $_FILES[$key]['type'];
//得到文件临时路径
$this->tmpName = $_FILES[$key]['tmp_name'];
//得到文件大小
$this->size = $_FILES[$key]['size'];
//得到文件后缀
$this->suffix = pathinfo($this->oldName)['extension'];
} /**
* 判断文件大小
*
* @return void
*/
protected function checkSize()
{
if ($this->size > $this->maxSize) {
$this->setOption('errorNumber', -3);
return false;
}
return true;
} /**
* 判断文件的mime是否符合
*
* @return void
*/
protected function checkMime()
{
if (!in_array($this->mime, $this->allowMime)) {
$this->setOption('errorNumber', -4);
return false;
}
return true;
} /**
* 判断文件的后缀是否符合
*
* @return void
*/
protected function checkSuffix()
{
if (!in_array($this->suffix, $this->allowSuffix)) {
$this->setOption('errorNumber', -5);
return false;
}
return true;
} /**
* 返回错误信息
*
* @return void
*/
protected function getErrorInfo()
{
switch ($this->errorNumber) {
case -1:
$str = '文件路径没有设置';
break;
case -2:
$str = '文件路径不是目录或者没有权限';
break;
case -3:
$str = '文件大小超过指定范围';
break;
case -4:
$str = '文件mime类型不符合';
break;
case -5:
$str = '文件后缀不符合';
break;
case -6:
$str = '不是上传文件';
break;
case -7:
$str = '文件上传失败';
break;
case 1:
$str = '文件超出php.ini设置大小';
break;
case 2:
$str = '文件超出html设置大小';
break;
case 3:
$str = '文件部分上传';
break;
case 4:
$str = '没有文件上传';
break;
case 6:
$str = '找不到临时文件';
break;
case 7:
$str = '文件写入失败';
break;
}
return $str;
}
}

测试代码:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>上传文件</title>
</head>
<body>
<form action="Upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="fm">
<br />
<input type="submit" value="上传">
</form>
</body>
</html>

运行结果:

PHP学习之文件上传类的更多相关文章

  1. MVC&WebForm对照学习:文件上传(以图片为例)

    原文  http://www.tuicool.com/articles/myM7fe 主题 HTMLMVC模式Asp.net 博客园::首页::  ::  ::  ::管理 5 Posts :: 0 ...

  2. ASP.NET 文件上传类 简单好用

    调用: UploadFile uf = new UploadFile(); /*可选参数*/ uf.SetIsUseOldFileName(true);//是否使用原始文件名作为新文件的文件名(默认: ...

  3. PHP 文件上传类

    FileUpload.;                $];                $_newname = date(,). :                             To ...

  4. php 文件上传类 实例分享

    最近在研究php上传的内容,找到一个不错的php上传类,分享下. <?php /** * 文件上传类 * class: uploadFile * edit: www.jbxue.com */ c ...

  5. [上传下载] C#FileUp文件上传类 (转载)

    点击下载 FileUp.zip 主要功能如下 .把上传的文件转换为字节数组 .流转化为字节数组 .上传文件根据FileUpload控件上传 .把Byte流上传到指定目录并保存为文件 看下面代码吧 // ...

  6. ThinkPHP文件上传类

    TP框架自带文件上传类使用: 类文件在ThinkPHP/Library/Think/默认在目录下 public function upload(){ $upload = new \Think\Uplo ...

  7. C#文件上传类,文件流,字节数组等

    using System;using System.IO;using System.Web;using System.Web.UI.WebControls; namespace DotNet.Util ...

  8. Php文件上传类class.upload.php

    简介 Class.upload.php是用于管理上传文件的php文件上传类, 它可以帮助你快速的给自己的网站集成上传文件功能.不仅如此,此分类还有一些列的处理功能,可以对上传的文件或者本地的文件进行处 ...

  9. 自定义MVC框架之工具类-文件上传类

    截止目前已经改造了3个类: ubuntu:通过封装验证码类库一步步安装php的gd扩展 自定义MVC框架之工具类-分页类的封装 该文件上传类功能如下: 1,允许定制上传的文件类型,文件mime信息,文 ...

随机推荐

  1. Cryptography -- 密码学

    Introduction to Cryptography Cryptography enables you to store sensitive information or transmit it ...

  2. SpringBoot中使用POI,快速实现Excel导入导出

    导出Excel 整体来说,Excel有.xls和.xlsx,那么在POI中这两个也对应两个不同的类,但是类名不同,方法基本都是一致的,因此我这里将只介绍.xls一种. 整体来说,可以分为如下七个步骤: ...

  3. leetcode-21.合并有序链表 · List

    题面 合并两个排序链表. 算法 创建结果链表头*res,*p指向头,当两个链表节点都不为空时,比较节点值,值小的挂在p后面,二者(p和小者)顺次后移.知道某条链表空,跳出while循环.接着,直接将不 ...

  4. C#基础 结构体、枚举

    一 结构体 结构体(struct)指的是一种数据结构,一个变量组,是一个自定义的集合.通常使用结构体创造新的“属性”,封装一些属性来组成新的类型.   结构体一般定义在Mian函数上面,位于Class ...

  5. Ubuntu 远程管理常用命令

    目标 关机/重启 shutdown 查看或配置网卡信息 ifconfig ping 远程登录和复制文件 ssh scp 01. 关机/重启 序号 命令 对应英文 作用 01 shutdown 选项 时 ...

  6. C和指针--命令行参数

    1.命令行参数 C程序的main函数具有两个形参,第1个通常称为argc,它表示命令行参数的数目.第2个称为argv,它指向一组参数值.由于参数的数目并没有内在的限制,所以argv指向这组参数值(本质 ...

  7. SignalR的三个Demo

    一.理解SignalR ASP .NET SignalR 是一个ASP .NET 下的类库,可以在ASP .NET 的Web项目中实现实时通信(即:客户端(Web页面)和服务器端可以互相实时的通知消息 ...

  8. Thymeleaf整合到Spring Security,标签sec不起作用

    将 pom 文件中的 thymeleaf-extras-springsecurity4 依赖改成  thymeleaf-extras-springsecurity5 <dependency> ...

  9. linux(3)

    一.用户和组的管理 Linux/Unix是多用户系统: root是超级用户,拥有最高权限.其它用户及权限由root来管理.对比Windows系统: 控制面板 -> 管理工具 -> 计算机管 ...

  10. Python多线程应用于自动化测试

    Python多线程应用于自动化测试 将多线程在测试巧妙地应用,确实会带来很多好处,并且这是充分利用机器资源执行高效率测试很好的方式 # -*- coding: utf-8 -*- import thr ...