wqc200
(wqc200)
新手上路

初级会员
UID 44387
精华
0
积分 11
帖子 9
金钱 11 喜悦币
威望 0
人脉 0
阅读权限 10
注册 2004-11-3
状态 离线
|
[广告]: q
m
共享一个上传文件类
现在项目用到的,能够对上传文件进行检查,并给出出错提示,个人认为比较人性化,欢迎拍专,并补充
<?php
/**
* 文件上传类
* @name upload.class.php
* @package core
* @subpackage class
* @author wuqingcheng <wqc200@163.com>
*/
/*
使用方法:by wuqingcheng <wqc200@163.com>
$upload = new upload(); //初始化
if($upload->__construct('image')) //检查是否上传了文件
{
$upload->checkFile(); //检查文件是否合法,可扩展
$upload->doUpload();
echo $upload -> errno; //错误代码
echo $upload -> errme; //错误信息
echo $upload -> uploadFileName; //上传后的文件名(包含路径)
}
*/
class upload {
/**
* 表单字段名
*/
var $file = array();
/**
* 文件上传错误代码(整型)
* 1 文件MIME类型错误
* 2 文件扩展名错误
* 3 文件大小超出限制大小
* 9 未知错误
*/
var $errno ;
/**
* 文件上传错误信息(文本)
*/
var $errme ;
/**
* 限制文件MIME类型
*/
//var $fileMimeType1 = array() ;
var $fileMimeType = array(
"jpg" => 'image/pjpeg',
"jpeg" => 'image/pjpeg',
"gif" => 'image/gif',
"png" => 'image/png',
"txt" => 'text/plain',
"html" => 'text/html',
"doc" => 'application/msword',
"mp3" => 'audio/mpeg',
"pdf" => 'application/pdf',
"rar" => 'application/rar',
"zip" => 'application/zip',
);
/**
* 限制文件扩展名
*/
var $fileExtName = array() ;
/**
* 文件最大尺寸
*/
var $maxSize = 2048000 ;
/**
* 文件前缀名
*/
var $filePrefixName ;
/**
* 上传后的文件名
*/
var $uploadFileName ;
function __construct()
{
$this->errno = '';
$this->errme = '';
}
/**
* 检查是否上传了图片
* @param array $_FILES['uploadFileName'] FILES数组<input type=file name="uploadFileName">
*/
function isUpload($_file)
{
if ($_FILES[$_file]['name'])
{
if ( !is_uploaded_file($_FILES[$_file]['tmp_name']) )
{
return false ;
}
else
{
$this-> file = $_FILES[$_file] ;
return true;
}
}
else
{
return false ;
}
}
/**
* @param string $savePath 保存上传文件的路径
* @param string $filename 有此值保存为指定文件名,否则取随机文件名
* @return boolean 如果上传成功返回TRUE 否则返回errno
*/
function doUpload($savePath="", $filename="")
{
$_extname = $this -> getExtName($this->file['name']) ;
$savePath = ($savePath=="") ? "./" : $savePath ;
$savePath = substr($savePath, -1, 1)=="/" ? $savePath : $savePath."/" ;
$base_dir = '';
$dir = explode('/', $savePath);
foreach($dir as $k => $v)
{
if($v === false)
{
continue;
}
$base_dir .= $v.'/';
if(!is_dir($base_dir))
{
mkdir($base_dir);
}
}
if (!$filename)
{
$fileName = $this -> filePrefixName . "_" . str_replace(" " , "" , str_replace("." , "" , microtime())).".". $_extname ;
}
else
{
$fileName = $this -> filePrefixName . $filename ;
}
$objfile = $base_dir.$fileName ;
if (!move_uploaded_file($this->file['tmp_name'], $objfile))
{
$this -> errno = 9 ;
return false ;
}
else
{
$this -> uploadFileName = $fileName ;
return true;
}
}
/**
* 取得文件的扩展名
* @param string $filename
* @return string
*/
function getExtName($filename)
{
return strtolower(substr(strrchr($filename, "."), 1));
}
/**
*判断单个文件是否符合法
* @return boolean 如果合法返回TRUE 否则返回errno
*/
function checkFile()
{
//上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值,或上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。
if(empty($this->file['name']))
{
return false ;
}
//判断文件扩展名
$_extname = $this -> getExtName($this->file['name']) ;
if (count($this->fileExtName))
{
if (!in_array($_extname, $this->fileExtName))
{
$this -> errno = 2 ;
$this -> errme = '您的文件格式不充许上传';
return false ;
}
}
//检查文件mime类型
//判断文件大小
if ($this->file['size'] > $this->maxSize)
{
$this -> errno = 3 ;
$this -> errme = '您的文件大小大于系统设置的大小';
return false ;
}
}
}
?>
|
|