http://cn.php.net/manual/en/features.file-upload.php

解决方案 »

  1.   

    <?php
    /*
    $storeDir = "相对路径,如 ./upload"形式
    */
    class Upload
    {
    var $_allowType;
    var $_allowSize;
    var $_storeDir;
    var $_userFile;


    function Upload($allowType="jpg|jpeg|gif|bmp",$allowSize,$storeDir,$userFile)
    {
    $this->_allowType = $allowType;
    $this->_allowSize = $allowSize;
    $this->_storeDir = $storeDir;
    $this->_userFile = $userFile;
    }

    function StartUpload()
    {
    if(!is_array($this->_userFile))
    {
    die("参数传递错误,请选择一个文件!"); 
    }
    if (!empty($this->_userFile['name']))
    {

    if($this->_userFile['size'] > $this->_allowSize)
    {
    die("文件太大,允许上传文件的大小为".$this->_allowSize);
    }
    $fileName = $this->_userFile['name'];
    if(!strstr($fileName,".")) 
    {
    $fileName .=".txt";
    $ext = "txt";

    }
    else
    {
    $ext = substr(strrchr($fileName,"."),1);
    if(!preg_match("/$ext/is",$this->_allowType))
    {
    die("您上传的文件类型不正确,充许的文件类型为".$this->_allowType);
    }
    }
    $newFileName = time().rand(1,10000).".".$ext; 

    if(!move_uploaded_file($this->_userFile['tmp_name'],$this->_storeDir."/".$newFileName)) 
    {
    die("复制文件失败");  
    }
    return $newFileName; 
    }
    }

    ?>