截止目前已经改造了3个类:

ubuntu:通过封装验证码类库一步步安装php的gd扩展

自定义MVC框架之工具类-分页类的封装

该文件上传类功能如下:

1,允许定制上传的文件类型,文件mime信息,文件大小

2,自由定制文件名是随机还是保持原来的文件名

3,谨慎的检查,友好的错误提示,精确定位文件上传哪一步出问题

测试结果:

ghostwu@ghostwu:~/php/senior_php/upload$ tree
.
├── 19502QV8-.jpg
├── Upload
│   ├── ghostwu_5a94beb3b2690.jpg
│   ├── ghostwu_5a94c727a1f1a.php
│   └── ghostwu_5a94c740615dc.html
├── upload.html
└── upload.php

upload.php:

 <meta charset="utf-8" />
<?php class Upload {
//上传路径
private $path = './Upload';
//允许的文件类型
private $allowFileType = [ 'jpg', 'png', 'jpeg', 'wbmp', 'gif' ];
//允许的mime信息
private $allowMime = [ 'image/jpeg', 'image/gif', 'image/png', 'image/wbmp' ];
//允许的最大文件大小
private $maxSize = 2048000;
//运行随机文件名
private $enableRandName = true;
//文件前缀
private $prefix = 'ghostwu_'; //错误号
private $errorNo;
//错误信息
private $errorInfo; //文件原来的名称
private $originName;
//新的文件名称
private $newName;
//文件后缀
private $suffix;
//文件大小
private $size;
//文件mime信息
private $mime;
//临时文件名
private $tmpName; public function __construct( $fileInfo = [] ){
foreach ($fileInfo as $k => $v ) {
$this->setProperty( $k, $v );
}
} //设置成员属性
public function setProperty( $k, $v ){
$property = array_keys( get_class_vars( __CLASS__ ) );
if( in_array( $k, $property ) ){
$this->$k = $v;
}
} public function __get( $key ) {
if( $key == 'errorNo' ) {
return $this->errorNo;
}else if ( $key == 'errorInfo' ) {
return $this->getErrorInfo();
}
} protected function getErrorInfo(){
$info = '';
switch( $this->errorNo ){
case 1000:
$info = '没有设置上传路径';
break;
case 1001:
$info = '上传路径不存在,或者没有写权限';
break;
case 1002:
$info = '文件大小超过限制';
break;
case 1003:
$info = '文件的mime信息不在允许范围内';
break;
case 1004:
$info = '不允许上传这种类型的文件';
break;
case 1006:
$info = '文件不是通过表单上传的';
break;
case 1007:
$info = '文件移动失败';
break;
case 1:
$info = '文件超出php.ini设置的大小';
break;
case 2:
$info = '超出html表单设置的大小';
break;
case 3:
$info = '文件只有部分被上传';
break;
case 4:
$info = '没有文件被上传';
break;
case 6:
$info = '找不到临时文件';
break;
case 7:
$info = '文件写入失败';
break;
} return $info;
} protected function check(){
if( !file_exists( $this->path ) || !is_dir( $this->path ) ){
return mkdir( $this->path, 0777, true );
}
if( !is_writable( $this->path ) ){
return chmod( $this->path, 0777 );
}
return true;
} public function upload( $name ){
//判断是否设置了上传路径
if( empty( $this->path ) ) {
$this->setProperty( 'errorNo', 1000 );
return false;
}
//判断路径是否存在,可写
if( !$this->check() ) {
$this->setProperty( 'errorNo', 1001 );
return false;
}
//判断上传文件是否错误,提取原文件信息
$errorno = $_FILES[$name]['error'];
if( $errorno ) {
$this->setProperty( 'errorNo', $errorno );
return false;
}else {
$this->getFileInfo( $name );
}
//判断文件是否符合上传要求(大小,后缀,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->setProperty( 'errorNo', 1007 );
return false;
}
}else {
$this->setProperty( 'errorNo', 1006 );
return false;
}
} protected function createNewName(){
if ( $this->enableRandName ) {
$name = $this->prefix . uniqid() . '.' . $this->suffix;
}else {
$name = $this->prefix . $this->originName;
}
return $name;
} protected function getFileInfo( $name ){
$this->originName = $_FILES[$name]['name'];
$this->mime = $_FILES[$name]['type'];
print_r( $this->mime );
$this->tmpName = $_FILES[$name]['tmp_name'];
$this->size = $_FILES[$name]['size'];
$this->suffix = pathinfo( $this->originName )['extension'];
} protected function checkSize(){
if( $this->size > $this->maxSize ) {
$this->setProperty( 'errorNo', 1002 );
return false;
}
return true;
} protected function checkMime(){
print_r( $this->allowMime );
if( !in_array( $this->mime, $this->allowMime ) ) {
$this->setProperty( 'errorNo', 1003 );
return false;
}
return true;
} protected function checkSuffix(){
if( !in_array( $this->suffix, $this->allowFileType ) ) {
$this->setProperty( 'errorNo', 1004 );
return false;
}
return true;
} } $upload = new Upload( [ 'maxSize' => 204800 ] );
$upload->setProperty( 'allowFileType', ['jpg','jpeg', 'gif', 'php', 'html'] );
$upload->setProperty( 'allowMime', [ 'image/jpeg', 'image/gif', 'image/wbmp', 'application/x-php', 'text/html' ] ); $upload->upload( 'photo' ); echo $upload->errorNo . '<br/>';
echo $upload->errorInfo . '<br/>'; ?>

upload.html

 <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>文件上传-by ghostwu</title>
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data" >
<input type="file" name="photo" id="photo" />
<input type="submit" name="upload" id="upload" value="上传" />
</form>
</body>
</html>

自定义MVC框架之工具类-文件上传类的更多相关文章

  1. Spring 4 官方文档学习(十一)Web MVC 框架之multipart(文件上传)支持

    http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-multipart 1.简 ...

  2. 自定义MVC框架之工具类-模型类

    截止目前已经改造了5个类: ubuntu:通过封装验证码类库一步步安装php的gd扩展 自定义MVC框架之工具类-分页类的封装 自定义MVC框架之工具类-文件上传类 自定义MVC框架之工具类-图像处理 ...

  3. 自定义MVC框架之工具类-图像处理类

    截止目前已经改造了4个类: ubuntu:通过封装验证码类库一步步安装php的gd扩展 自定义MVC框架之工具类-分页类的封装 自定义MVC框架之工具类-文件上传类 图像处理类: 1,图片加水印处理( ...

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

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

  5. ThinkPHP文件上传类

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

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

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

  7. PHP 文件上传类

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

  8. node.js 在 Express4.0 框架使用 Connect-Busboy 实现文件上传

    node.js下四种post提交数据的方式 今天说分享的是其中一种,就是上传文件. Express 4.0 以后,将功能原子化,高内聚,低耦合,独立出了很多中间件 今天主要分享文件上传 对于conne ...

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

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

随机推荐

  1. 微信小程序实现给循环列表点击添加类(单项和多项)

    在微信小程序里面没有DOM对象, 不能操作DOM. 所有的操作通过数据来实现,下面主要实现了给循环列表点击添加类的操作 一.单项 目标需求:实现下图,给点击的view增加类,每次只能选择一个. 主要思 ...

  2. Dell R730服务器 Raid5配置

    Dell R730服务器,有7块5t硬盘,默认做的RAID5.我们的目的是取其中6块硬盘做RAID5,留一块硬盘做热备. 一块SSD系统盘. 在这里,我具体解释一下 ①6块硬盘做成RAID5 ②6块硬 ...

  3. Spring框架的演变

    什么是Spring 如果想要解释Spring,那么最难的部分就是对其进行分类.通常情况下,Spring被描述为构建Java应用程序的轻量级框架,但这种描述带来了两个有趣的观点. 首先,与许多其他框架( ...

  4. Linux在终端和控制台下复制粘贴命令快捷键

    1.在终端下: (1)复制命令:Ctrl + Shift + C 组合键. (2)粘贴命令:Ctrl + Shift + V 组合键. 2.在控制台下:(即vi编辑过程中) (1)复制命令:Ctrl ...

  5. 02-02:springboot 整合filter

    1.通过注解扫描完成Filter组件的注册 1.1编写filter (添加拦截的servlet) //@WebFilter(filterName = "FirstFilter",u ...

  6. Android get current Locale, not default

    he default Locale is constructed statically at runtime for your application process from the system ...

  7. php -- 文件读写

    ----- 024-file.php ----- <!DOCTYPE html> <html> <head> <meta http-equiv="c ...

  8. JavaScript -- URL编码

    ----- 010-escape.html ----- <!DOCTYPE html> <html> <head> <meta http-equiv=&quo ...

  9. 【原创】贡献一个项目中用到的js身份证验证-超级准!!!

    前言 百度百科解释:身份证号码 首先贡献一个大神的链接:js验证身份证超准 代码 function checkIdcard(idcard) { var Errors = new Array( &quo ...

  10. 哨兵/sentinel:在算法设计中的应用

    哨兵(sentinel)昨天看算法导论里对哨兵的描述后,觉得这是一种很有意思的编程思想.哨兵是一个哑对象.一般哨兵不存放任何数据,但其结构体与其他有用的元素一致.正如其字面意思,哨兵是在边界保卫祖国的 ...