[code=PHP
class upfile{
  public $file_name;
    public $save_name;
    public $save_path; //必须设置的选项
    public $file_format = ""; //类似jpg,gif,png的形式
    public $over_write = 0; //是否覆盖同名文件,0,不,1,是
    public $max_size = 210000000;
    public $file_ext;
    public $err_no = 0;
 
function upload($form, $file = "")
{
  if($this->save_path == '')
{
$this->save_path = './';
}

if(!isset($_FILES[$form]))
  {
    $this->halt("指定的文件名称不存在。");
   }
   else
   {
    $files = $_FILES[$form];
  }
  
   if(!is_writable($this->save_path))
   {
    $this->halt("指定的路径不可写。");
   }
  
   if(is_array($files["name"]))
   {
    for($i = 0; $i < count($files["name"]); $i++)
    {
     $arr["name"] = $files["name"][$i];
     $arr["tmp_name"] = $files["tmp_name"][$i];
     $arr["size"] = $files["size"][$i];
     $arr["error"] = $files["error"][$i];
    
     $this->get_file_ext($arr["name"]);
     $this->set_save_name();
     $this->copy_file($arr);
    }
   }
   else
   {
    $this->get_file_ext($files["name"]); //取得扩展名
    $this->set_save_name($file); //设置保存文件名
    $this->copyfile($files);
   }
   return true;
}
 
function copyfile($files)
  {
   if($files["size"] > $this->max_size)
   {
   $this->halt("上传文件 ".$files["name"]." 大小超出系统限定值[".$this->max_size." 字节],不能上传。");
   }
  
   if(!$this->over_write && file_exists($this->save_name))
   {
    $this->halt($this->save_name." 文件名已经存在。");
   }
  
   if($this->file_format != "" && !in_array(strtolower($this->file_ext), explode(",", strtolower($this->file_format))))
   {
    $this->halt($this->file_ext." 文件格式不允许上传。");
   }
  
   if(!copy($files["tmp_name"], $this->save_path.$this->save_name))
   {
    $errors = array(
    0=>"文件上传成功",
        1=>"上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值。 ",
        2=>"上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。 ",
        3=>"文件只有部分被上传。 ",
        4=>"没有文件被上传。 "
        );
    $this->halt($errors[$files["error"]]);
   }
   else
   {
    @unlink($files["tmp_name"]);
   }
}
 
function get_file_ext($file_name)
{
if($file_name == "") 
return;
   $ext = explode(".", $file_name);
   $this->file_ext = $ext[1];
}
 
    function set_save_name($save_name = "")
    {
        if ($save_name == "")  
        {
//生成日期文件名
$name = $this->get_system_name().'.'.$this->file_ext;

//则生成一个随机文件名
            /*
    srand ((double) microtime() * 1000000);
            $rnd = rand(100,999);
            $name = date('U') + $rnd;
            $name = $name.".".$this->ext;
*/

//生成随机混合文件名
//$name = $this->get_random_name(8);
        } 
else 
{
            $name = $save_name;
        }
        $this->save_name = $name;
    }
 
function halt($msg)
{
   echo "<strong>Error: </strong><br />".$msg;
   exit;
  }

///////////////////////////////////////////////////////////////////////////////////////////////

function get_random_name($len)
{
$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0129456789abcdefghijklmnopqrstuvwxyz_@#-+';
$s = '';
for($i = 0; $i <$len; $i++)
{
$s .= $str[rand(0,67)];
}

return $s . $this->file_ext;
}

function get_system_name()
{
$s = date('Ymdhis').$this->fiel_ext;
return $s;
}
}
[/code]调用方法:<?php  require_once 'inc.class.upfile.php' ?>
<?php 
if($_FILES['upfile'])
{
$f = new upfile();
//$f->save_path = '../../images/upload';
$f->upload('files');      //files 为file表单
}
?>
顺便宣传下俺正在写的小说:http://blog.csdn.net/arserangel