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

截止目前已经改造了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>
上一篇:UVa 147 Dollars(完全背包)


下一篇:HTML5 本地数据库(SQLite) 示例