<?php class upload{ private $_name; /* 存放上传文件名称,这里上传的是一个数组 */ private $_bookpic; /*存放上传图片的路径*/ private $_type; /*存放允许上传文件的类型*/ private $_size; /*存放上传文件允许大小*/ private $_upfiletype; /*存放系统上传允许的文件类型*/ public function __construct($name,$bookpic,$size,$type) { $this->_name = $name; $this->_bookpic = $bookpic; $this->_size = $size; $this->_upfiletype = $type; } public function uploadfile () { for($i=0;$i<=count($this->_name[name]);$i++) { if($this->_name[error][$i]==0) { $name = $this->_name[name][$i]; $type = $this->_name[type][$i]; $tmp_name = $this->_name[tmp_name][$i]; $size = $this->_name[size][$i]; $error = $this->_name[error][$i]; if($this->getSizeExt($size) and $this->getTypeExt($type)) { /*通过取得文件的扩展名并且通过时间进行重新命名,以防止出现重复图片名称*/ $strname= time().$i.".".$this->getFileExt($name); //这里通过加$i变量是防止图片名称重新命名 move_uploaded_file($tmp_name,$this->_bookpic.$strname); } } } } /*取得文件的扩展名*/ public function getFileExt($name) { $stuff= pathinfo($name); return $stuff['extension']; } /*比较上传文件的大小与系统规定上传文件的大小*/ public function getSizeExt($getSize) { if($getSize < $this->_size) { return true; }else{ $this->error_msg('上传的文件超过系统默认设置的大小了!'); exit(); } } /*进行用户上传的文件类型与上传系统的文件类型做比较,如果符合系统自定义的文件类型,在上传,否则打印出错误*/ public function getTypeExt($extension) { foreach($this->_upfiletype as $uplistfile) { if(strcasecmp($extension, $uplistfile) == 0 ) { return true; } } return false; } /*返回信息参数*/ public function error_msg($msg) { echo $msg; } } ?> 以下是调用方面的程序,在本机测试了没有问题? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <?php if($_POST['submit']){ include_once("upfile.php"); $file=$_FILES['thumb']; $dir='pic/'; if(!file_exists($dir)){ mkdir($dir); } $size=5000000; $type=array('image/pjpeg','image/gif'); $uploadfile=new upload($file,$dir,$size,$type); $uploadfile->uploadfile(); } ?> <form action="5.php" method="post" enctype="multipart/form-data"> <input type="file" name="thumb" /><br/> <input type="submit" name="submit" value="提交" /> </form> </body> </html>