最近写了一个文件上传类,支持单个上传,多个上传,限制上传类型,限制上传大小,是否改变上传文件名,自定义上传目录功能;
新建类文件FileUpload.class.php,话不多说,上代码,
<?php
class FileUpload{
private $allowType = array(); //允许上传类型
private $maxSize; //文件大小
private $isRename; //是否改名
private $uploadPath; //上传路径
private $fileField; //上传文件表单名称
private $errorMessage; //错误信息归纳
private $originName; //文件原名称
private $newFileName; //文件新名称
private $newPathFileName; //新文件名称带路径
public function __construct($fileField,$allowType=array(),$maxSize=10000,$isRename=false,$uploadPath="uploads"){
$this->allowType = $allowType;
$this->maxSize = $maxSize;
$this->isRename = $isRename;
$this->uploadPath = $uploadPath;
$this->fileField = $fileField;
}
//上传验证
public function upload(){
if(!$this->checkUploadPath()){
return false;
}
$name = $_FILES[$this->fileField]['name'];
$tmp_name = $_FILES[$this->fileField]['tmp_name'];
$size = $_FILES[$this->fileField]['size'];
$error = $_FILES[$this->fileField]['error'];
$name_total = count($name);
//如果是多文件上传
if(is_array($name)){
$errors = array(); //错误数组
$filenames = array(); //上传成功的新文件名数组
$return = true;
for($i=0;$i<$name_total;$i++){//循环所有获得的上传文件
$fileTypeStr = explode('.', $name[$i]);
$fileType = strtolower($fileTypeStr[count($fileTypeStr)-1]); //获取文件类型
if($error[$i]){ //如果此值为0,说明无错
$this->getErrorMessage($error[$i]);
$errors[] = $this->errorMessage;
$return = false;
}
if(!$this->checkFileType($fileType) || !$this->checkSize($size[$i])){
$errors[] = $this->errorMessage;
$return = false;
}
}
if(!$return){ //如果为false,说明有错
$this->errorMessage = $errors;
return false;
}else{//无错则循环上传
for($i=0;$i<$name_total;$i++){
$this->originName = $name[$i]; //文件原名称
if($this->copyFile($tmp_name[$i],$fileType)){//传入临时文件名,文件类型
$filenames[] = $this->newFileName;
}
else{
$errors[] = $this->errorMessage;
$return = false;
}
}
}
//如果$return为false,获取所有错误值,返回false;如果$return为true,获取所有上传后的新文件名,返回true
if(!$return){
$this->errorMessage = $errors;
return false;
}else{
$this->newFileName = $filenames;
return true;
}
}else{//如果单个文件上传
$this->originName = $name; //文件原名称
$fileTypeStr = explode('.', $name);
$fileType = strtolower($fileTypeStr[count($fileTypeStr)-1]); //获取文件类型
if(!$error){ //为0说明成功,不为0则失败,但0为false,所以此时加!表示成功
if(!$this->checkFileType($fileType) || !$this->checkSize($size)){
return false;
}else{
if($this->copyFile($tmp_name, $fileType)){
return true;
}else{
return false;
}
}
}else{
$this->getErrorMessage($error);
return false;
}
}
}
//获取错误信息
public function getErrorMessage($errorNum){
$this->errorMessage = "上传文件<font style='color:red;'>$this->originName</font>时出错:";
switch ($errorNum){
case 4: $this->errorMessage .= '没有文件被上传'; break;
case 3: $this->errorMessage .= '文件只有部分被上传'; break;
case 2: $this->errorMessage .= '上传文件的大小超过了 HTML表单中MAX_FILE_SIZE选项指定的值'; break;
case 1: $this->errorMessage .= '上传的文件超过了 php.ini中upload_max_filesize(默认情况为2M) 选项限制的值'; break;
case -1: $this->errorMessage .= '未允许的类型'; break;
case -2: $this->errorMessage .= "上传文件大小已超过{$this->maxSize}个字节"; break;
case -3: $this->errorMessage .= $this->newFileName.'文件上传失败'; break;
case -4: $this->errorMessage .= '上传文件目录'.$this->uploadPath.'创建失败,请重新指定上传目录'; break;
default: $this->errorMessage .= '其他错误';
}
}
//检查存放上传文件目录是否存在和写权限
private function checkUploadPath(){
if(!file_exists($this->uploadPath) || !is_writable($this->uploadPath)){
if(!mkdir($this->uploadPath,0777,true)){
$this->getErrorMessage(-4);
return false;
}else{
return true;
}
}else{
return true;
}
}
//检查上传文件类型
private function checkFileType($type){
if(empty($this->allowType) || in_array($type, $this->allowType)){
return true;
}else{
$this->getErrorMessage(-1);
return false;
}
}
//检查上传文件尺寸
private function checkSize($size){
if($size>$this->maxSize){
$this->getErrorMessage(-2);
return false;
}else{
return true;
}
}
//新的文件名称
private function getNewFileName($type){
if($this->isRename){
$this->newFileName = date("Ymd").'_'.rand(1000, 9999).'.'.$type;
}else{
$this->newFileName = $this->originName;//将文件名转码为gbk以支持汉字,window上传默认为ascll编码,先转码.不然只能支持英文名
}
$this->newPathFileName = $this->uploadPath.'/'.$this->newFileName;
return $this->newPathFileName; //返回最终的新文件名
}
//文件上传操作
private function copyFile($filename,$type){
if(move_uploaded_file($filename, mb_convert_encoding($this->getNewFileName($type),'gbk'))){
return true;
}else{
$this->getErrorMessage(-3);
return false;
}
}
//获取属性值
public function getField($key){
return $this->$key;
}
}
基本上在类中已经写了很清楚的注释了,下面就是外部代码调用的问题了
注意:include 引入上传类文件
$logoUp = new FileUpload('slogo',array('jpg','png'),110000,false);
if($logoUp->upload()){
print_r($logoUp->getField('newFileName'));
}else{
print_r($logoUp->getField('errorMessage'));
}
试试吧,如果成功将打印上传后的文件名,不成功则打印错误信息