<?php
/**
* 使用例程
* include 'fileupload.class.php';
* $upload = new fileupload;
* $upload -> set("path","./bbbb/")//上传路径
* -> set("maxSize",10000000)//上传文件最大的大小
* -> set("fileType",array("jpg","gif","png"))//上传文件的类型 默认支持所有
* -> set("isRandName",true);//是否需要随机文件名
*
* if( $upload -> upload("my") ){
* var_dump($upload -> getUplodeMsg());
* }else{
* var_dump($upload -> getUplodeMsg());
* }
*
* <form action="" method="POST" enctype="multipart/form-data">
* <input type="hidden" name="MAX_FILE_SIZE" value="10240000">
* <input type="file" name="my[]"><br />
* <input type="file" name="my[]"><br />
* <input type="file" name="my[]"><br />
* <input type="submit" value="文件上传">
* </form>
*
*/
class fileupload
{
private $path;//上传路径
private $maxsize;//上传文件最大的大小
private $filetype;//上传文件的类型 默认支持所有
private $israndname;//是否需要随机文件名
private $fileMsg=array();
private $name;
private $tmp_name;
private $size;
private $error;
private $hz=array();
function __construct($_path="./upload/",$_maxSize=2048000,$_flieType="*",$_isRandName=true)
{
$this->path=$_path;
$this->maxSize=$_maxSize;
$this->fileType=$_flieType;
$this->isRandName=$_isRandName;
}
function set($key,$val){
$key = strtolower($key);
if( array_key_exists( $key, get_class_vars(get_class($this) ) ) ){
$this->$key = $val;
}
return $this;
}
function upload($inputName){
if(empty($_FILES[$inputName])){
return false;
}
if(!$this->checkFliePath()){
return false;
}
$this->name=$_FILES[$inputName]['name'];
$this->tmp_name=$_FILES[$inputName]['tmp_name'];
$this->size=$_FILES[$inputName]['size'];
$this->error=$_FILES[$inputName]['error'];
if(!$this->checkError()){
return false;
}
if(!$this->checkMaxSize()){
return false;
}
if(!$this->checkFileType()){
return false;
}
if(!$this->checkUploadFile()){
return false;
}
if(!$this->uploadFile()){
return false;
}
return true;
}
private function uploadFile()
{
$rt=true;
if(is_array($this->name)){
for ($i=0; $i < count($this->name); $i++) {
$newFileName = $this->isRandName ?
date("YmdHis").rand(100,999).'.'.$this->hz[$i] :
$this->name[$i] ;
$newFilePath = $this->path.$newFileName;
if (!move_uploaded_file($this->tmp_name[$i],$newFilePath)) {
$this->fileMsg[$i]['error'] = "上传的文件移动失败" ;
}else{
$this->fileMsg[$i]['error'] = true;
$this->fileMsg[$i]['newName'] = $newFileName;
$this->fileMsg[$i]['path'] = $newFilePath;
}
if ($this->fileMsg[$i]['error'] != true) {
$rt=false;
}
}
return $rt;
}
}
private function checkUploadFile(){
$rt=true;
if(is_array($this->name)){
for ($i=0; $i < count($this->name); $i++) {
if (!is_uploaded_file($this->tmp_name[$i])) {
$this->fileMsg[$i]['error'] = "不是通过http post上传的" ;
}else{
$this->fileMsg[$i]['error'] = true;
}
if ($this->fileMsg[$i]['error'] != true) {
$rt=false;
}
}
return $rt;
}
}
private function checkFileType(){
$rt=true;
if(is_array($this->name)){
for ($i=0; $i < count($this->name); $i++) {
$hz=explode('.', $this->name[$i]);
$hz=$hz[count($hz)-1];
$this->hz[$i]=$hz;
if(!$this->fileType="*"){
if (!in_array($this->hz[$i],$this->fileType)) {
$this->fileMsg[$i]['error'] = "不允许上传的文件类型" ;
}else{
$this->fileMsg[$i]['error'] = true;
}
}else{
$this->fileMsg[$i]['error'] = true;
}
if ($this->fileMsg[$i]['error'] != true) {
$rt=false;
}
}
return $rt;
}
}
/*检查上传大小是否符合*/
private function checkMaxSize()
{
$rt=true;
if(is_array($this->name)){
for ($i=0; $i < count($this->name); $i++) {
$this->fileMsg[$i]['error'] = ($this->size[$i] > $this->maxSize) ?
"文件大小超过自定义设置的限定值" :
true ;
if ($this->fileMsg[$i]['error'] != true) {
$rt=false;
}
}
return $rt;
}
}
/*检查上传文件是否有错误*/
private function checkError()
{
$rt=true;
if(is_array($this->name)){
for ($i=0; $i < count($this->name); $i++) {
$this->fileMsg[$i]['name'] = $this->name[$i];
$this->fileMsg[$i]['error'] = $this->checkFileError($this->error[$i]);
if ($this->fileMsg[$i]['error'] != true) {
$rt=false;
}
}
return $rt;
}
}
function getUplodeMsg()
{
return $this->fileMsg;
}
private function setUplodeMsg($name,$newName="",$path="",$size="",$error="")
{
$msg['name']=$name;
$msg['newName']=$newName;
$msg['size']=$size;
$msg['path']=$path;
$msg['error']=$error;
$this->fileMsg[]=$msg;
}
/*检测错误*/
private function checkFileError($error)
{
if($error > 0){
$errorMsg = array( 1 => "上传文件大小超出了php配置文件中的约定值: upload_max_filesize",
2 => "上传文件大小超出了表单中的约定值: MAX_FILE_SIZE",
3 => "文件只有部分被上载",
4 => "没有上传任何文件",
);
$msg=($error >= 5)?
"未知错误 错误码:".$error :
$errorMsg[$error] ;
}else{
$msg=true;
}
return $msg;
}
/*检查上传文件夹是否存在*/
private function checkFliePath()
{
if (!file_exists($this->path)) {
if(!@mkdir($this->path,0755)){
return false;
}
}
return true;
}
}
?>
上面是写好的类,写完后发现..只支持多文件上传,但是哩表单的name后面给[]还是可以的.
下面是写多文件上传前的单文件上传代码
var_dump(upload("myfile","./b/",array("jpg","gif","png","zip","rar"),10000000,true));
function upload($inputName,$path,$fileType,$maxSize,$isRandName){
if(empty($_FILES[$inputName])){
$fileMsg['error']="没有上传任何文件";
return $fileMsg;
}
//上传目录不存在就创建
if (!file_exists($path)) {
if(!@mkdir($path,0755)){
$fileMsg['error']="上传目录不存在并且创建失败!";
return $fileMsg;
}
}
//echo "<pre>";
//var_dump($_FILES[$inputName]);
//echo "</pre>";
$fileMsg['name']=$_FILES[$inputName]['name'];
if($_FILES[$inputName]['error'] > 0){
switch ($_FILES[$inputName]['error']) {
case 1:
$msg = "上传文件大小超出了php配置文件中的约定值: upload_max_filesize";
break;
case 2:
$msg = "上传文件大小超出了表单中的约定值: MAX_FILE_SIZE";
break;
case 3:
$msg = "文件只有部分被上载";
break;
case 4:
$msg = "没有上传任何文件";
break;
default:
$msg = "未知错误 错误码:".$error;
break;
}
$fileMsg['error'] = $msg ;
return $fileMsg;
}
if ($_FILES[$inputName]['size'] > $maxSize) {
$fileMsg['error'] = "文件大小超过自定义设置的限定值" ;
return $fileMsg;
}
$hz=explode('.', $_FILES[$inputName]['name']);
$hz=$hz[count($hz)-1];
if (!in_array($hz,$fileType)) {
$fileMsg['error'] = "不允许上传的文件类型" ;
return $fileMsg;
}
if (!is_uploaded_file($_FILES[$inputName]['tmp_name'])) {
$fileMsg['error'] = "不是通过http post上传的" ;
return $fileMsg;
}
//设置上传的文件名
$newFileName = $isRandName ?
date("YmdHis").rand(100,999).'.'.$hz :
$_FILES[$inputName]['name'] ;
$newFilePath = $path.$newFileName;
if (!move_uploaded_file($_FILES[$inputName]['tmp_name'],$newFilePath)) {
$fileMsg['error'] = "上传的文件移动失败" ;
return $fileMsg;
}
$fileMsg['name'] = $_FILES[$inputName]['name'];
$fileMsg['newName'] = $newFileName;
$fileMsg['size'] = $_FILES[$inputName]['size'];
$fileMsg['path'] = $newFilePath;
//$fileMsg['error'] = "";
return $fileMsg;
}
还有一个是写多文件上传类的时候写的单个函数,
echo "<pre>";
var_dump(uploads("my","./b/",array("jpg","gif","png","zip","rar"),10000000,true));
echo "</pre>";
function uploads($inputName,$path,$fileType,$maxSize,$isRandName){
//上传目录不存在就创建
if (!file_exists($path)) {
if(!@mkdir($path,0755)){
$fileMsg['error']="上传目录不存在并且创建失败!";
return $fileMsg;
}
}
$name=$_FILES[$inputName]['name'];
$tmp_name=$_FILES[$inputName]['tmp_name'];
$size=$_FILES[$inputName]['size'];
$error=$_FILES[$inputName]['error'];
$fileMsg=array();
if(is_array($name)){
$return=true;
for ($i=0; $i < count($name); $i++) {
$_fileMsg['name']=$name[$i];
$_fileMsg['error']=fileError($error[$i]);
if ($size[$i] > $maxSize) {
$_fileMsg['error'] = "文件大小超过自定义设置的限定值" ;
}
$hz=explode('.', $_fileMsg['name']);
$hz=$hz[count($hz)-1];
if (!in_array($hz,$fileType)) {
$_fileMsg['error'] = "不允许上传的文件类型" ;
}
if (!is_uploaded_file($tmp_name[$i])) {
$_fileMsg['error'] = "不是通过http post上传的" ;
}
$fileMsg[]=$_fileMsg;
if ($_fileMsg['error']!='') {
$return=false;
}
}
if (!$return) {
return $fileMsg;
}else{
for ($i=0; $i < count($name); $i++) {
$hz=explode('.', $name[$i]);
$hz=$hz[count($hz)-1];
$newFileName = $isRandName ?
date("YmdHis").rand(100,999).'.'.$hz :
$name[$i]['name'] ;
$newFilePath = $path.$newFileName;
if (!move_uploaded_file($tmp_name[$i],$newFilePath)) {
$fileMsg[$i]['error'] = "上传的文件移动失败" ;
}
//$fileMsg[$i]['name'] = $name[$i];
$fileMsg[$i]['newName'] = $newFileName;
$fileMsg[$i]['size'] = $size[$i];
$fileMsg[$i]['path'] = $newFilePath;
}
return $fileMsg;
}
}
}
function fileError($error){
if($error > 0){
switch ($error) {
case 1:
$msg = "上传文件大小超出了php配置文件中的约定值: upload_max_filesize";
break;
case 2:
$msg = "上传文件大小超出了表单中的约定值: MAX_FILE_SIZE";
break;
case 3:
$msg = "文件只有部分被上载";
break;
case 4:
$msg = "没有上传任何文件";
break;
default:
$msg = "未知错误 错误码:".$error;
break;
}
return $msg;
}
return '';
}