uploadtype=$type;
$this->file=$file;
}
private function createfilename()
{
return $this->filename=ttr_.time().$this->getrandomn(4);
}
private function getuploadpath()
{
if(substr($this->uploadpath,-1,1)!=/)
{
$this->savepath=$this->uploadpath./.date(ym);
}else{
$this->savepath=$this->uploadpath.date(ym);
}
$this->savepath=$this->getfolder($this->savepath);
return true;
}
private function getfileext($tempfilename)
{
return end(explode(.,$tempfilename));
}
private function getext()
{
if(in_array(strtolower($this->getfileext($tempfilename)),$this->ext))
{
return true;
}else{
return false;
}
}
private function getfolder($folder)
{
if(!is_dir($folder))
{
mkdir($folder);
}
return $folder./;
}
public function upload()
{
if($this->uploadtype==one)
{
if($this->getext($this->file[type]))
{
parent::errorext();
}else if($this->file[size]>self::filesize){
parent::errorfilesize();
}else if(!$this->getuploadpath()){
parent::erroruploadpath();
}else{
$filenametemp=$this->createfilename();
$filename=$this->savepath.$filenametemp...$this->getfileext($this->file[name]);
if(move_uploaded_file($this->file[tmp_name],$filename))
{
$this->uploadfilename=$filenametemp;
parent::okmoved();
}else{
parent::errormoveupload();
}
}
}else if($this->uploadtype==any){
for($i=0;$icount($this->file);$i++)
{
if($this->getext($this->file[$i][type]))
{
parent::errorext();
}else if($this->file[$i][size]>self::filesize){
parent::errorfilesize();
}else if(!$this->getuploadpath()){
parent::erroruploadpath();
}else{
$filenametemp=$this->createfilename();
$filename=$this->savepath.$filenametemp...$this->getfileext($this->file[$i][name]);
if(move_uploaded_file($this->file[$i][tmp_name],$filename))
{
$str.=$filenametemp.|;
}else{
parent::errormoveupload();
}
}
}
$this->uploadfilename=substr($str,0,strlen($str)-1);
parent::okmoved();
}
}
public function getuploadfilename()
{
return $this->uploadfilename;
}
public function setuploadpath($path)
{
$this->uploadpath=$path;
}
private function getrandomn($n)
{
if ($n 1 || $n>10) return ;
$ary_num= array(0,1,2,3,4,5,6,7,8,9);
$return =;
for ($i=0;$i$n;$i++)
{
$randn = rand(0,9-$i);
$return .= $ary_num[$randn];
$ary_num[$randn] = $ary_num[9-$i];
}
return $return;
}
public function __destruct()
{
$this->uploadfilename=null;
$this->uploadtype=null;
$this->file=null;
$this->savepath=null;
}
}
class error
{
public static function errorfilesize()
{
echo 超出最大上传限制;
}
public static function errorext()
{
echo 此类文件不允许上传;
}
public static function erroruploadpath()
{
echo 上传路径不正确;
}
public static function errormoveupload()
{
echo 上传失败;
}
public static function okmoved()
{
echo 上传成功!;
}
public static function okarraymoved()
{
echo 上传成功!;
}
}
实例应用:
upload();
echo $upload->getuploadfilename();
}
?>
nbsp;html public -//w3c//dtd xhtml 1.0 transitional//en http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd>
untitled document