PHP文件上传 $_FILES['file']['error']从 PHP 4.2.0 开始,PHP 将随文件信息数组一起返回一个对应的错误代码。该代码可以在文件上传时生成的文件数组中的 error 字段中被找
到,也就是 $_FILES['file']['error']。
UPLOAD_ERR_OK 其值为 0,没有错误发生,文件上传成功。 UPLOAD_ERR_INI_SIZE 其值为 1,上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值。 UPLOAD_ERR_FORM_SIZE 其值为 2,上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。 UPLOAD_ERR_PARTIAL 其值为 3,文件只有部分被上传。 UPLOAD_ERR_NO_FILE 其值为 4,没有文件被上传。 UPLOAD_ERR_NO_TMP_DIR 其值为 6,找不到临时文件夹。PHP 4.3.10 和 PHP 5.0.3 引进。 UPLOAD_ERR_CANT_WRITE 其值为 7,文件写入失败。PHP 5.1.0 引进。 注意: 以上值在 PHP 4.3.0 之后变成了 PHP 常量。 <?php /**************************** 上传类 By:donnier ****************************/ class upload_class{ private $ptname; //上传表单名称; private $udname; //是否以月份建立子目录(0为否,其他为真); private $ufname; //是否以时间建立文件名(0为否,其他为真); private $ultype; //上传文件类型; private $ulsize; //上传文件大小; private $ulname; //输出文件名称; private $ulpath; //输出文件路径; private $wm; //水印附加(0为不加,其他为加); private $wmtype; //水印类型(0为文字,其他为图片); private $wmpic; //水印图片; private $wmpicquality; //图片质量; private $wmpictrans; //图片透明; private $wmstr; //水印字符; private $wmstrsize; //字符大小; private $wmstrfont; //字符字体; private $wmstrcolor; //字符颜色; private $wmpos; //水印位置(1为顶端居左,2为顶端居中,3为顶端居右……); function __construct($ptname='upfile',$udname=1,$ufname=1,$ultype=array('image/jpg','image/jpeg','image/png','image/pjpeg','image/gif','image/bmp',
'image/x-png'),$wm=1,$wmtype=1,$wmpic='images/wm.gif',$ulsize=2097152,$ulpath='images/temp/',$wmpictrans=20,$wmpicquality=80,$wmstr='DONLINE',
$wmstrsize=5,$wmstrfont='./font/cour.ttf',$wmstrcolor='#ff0000',$wmpos=9)
{ $this->ptname=$_FILES[$ptname]; $this->udname=$udname; $this->ufname=$ufname; $this->ultype=$ultype; $this->ulsize=$ulsize; $this->ulpath=$ulpath; $this->wm=$wm; $this->wmtype=$wmtype; $this->wmpic=$wmpic; $this->wmpicquality=$wmpicquality; $this->wmpictrans=$wmpictrans; $this->wmstr=$wmstr; $this->wmstrsize=$wmstrsize; $this->wmstrfont=$wmstrfont; $this->wmstrcolor=$wmstrcolor; $this->wmpos=$wmpos; } function uploadfun(){ if($_SERVER['REQUEST_METHOD']=='POST'){ if(!is_uploaded_file($this->ptname['tmp_name']))
$this->errorfun('上传失败!'); if(!in_array($this->ptname['type'],$this->ultype))
$this->errorfun('不支持的文件类型!'); if($this->ulsize<$this->ptname['size'])
$this->errorfun('文件太大!'); if($this->udname)
{
date_default_timezone_set('UTC');$this->ulpath=$this->ulpath.'month_'.date('Ym').'/';
} else
{
$this->ulpath=$this->ulpath;
}
$this->createfun($this->ulpath);
if($this->ufname)
{
$t=pathinfo($this->ptname['name']);
$this->ulname=$this->ulpath.time().'.'.$t['extension'];
} else
{
$this->ulname=$this->ulpath.$this->ptname['name'];
} if(file_exists($this->ulname))
$this->errorfun('该文件已存在!'); if(!move_uploaded_file($this->ptname['tmp_name'],$this->ulname))
$this->errorfun('移动文件错误!'); $this->wmfun(); $this->errorfun('上传成功!'); } } function createfun($d){ if(!file_exists($d)){$this->createfun(dirname($d));mkdir($d);} } function wmfun(){ if($this->wm){ if(file_exists($this->ulname)){ $groundimg=getimagesize($this->ulname); $ow=$groundimg[0]; $oh=$groundimg[1]; switch($groundimg[2]){ case 1:$g=imagecreatefromgif($this->ulname);break; case 2:$g=imagecreatefromjpeg($this->ulname);break; case 3:$g=imagecreatefrompng($this->ulname);break; case 4:$g=imagecreatefromwbmp($this->ulname);break; default:$this->errorfun('不支持的背景图片类型!'); } } else{$this->errorfun('背景图片不存在!');} if(file_exists($this->wmpic)){ $wmimg=getimagesize($this->wmpic); $ww=$wmimg[0]; $wh=$wmimg[1]; switch($wmimg[2]){ case 1:$w=imagecreatefromgif($this->wmpic);break; case 2:$w=imagecreatefromjpeg($this->wmpic);break; case 3:$w=imagecreatefrompng($this->wmpic);break; case 4:$w=imagecreatefromwbmp($this->wmpic);break; default:$this->errorfun('不支持的水印图片类型!'); } } else{$this->errorfun('水印图片不存在!');} switch($this->wmtype){ case 0:$tp=imagettfbbox(ceil($this->wmstrsize*2.5),0,$this->wmstrfont,$this->wmstr);$ww=$tp[2]-$tp[6];$wh=$tp[3]-$tp[7];unset($tp);break; case 1:$ww=$ww;$wh=$wh;break; default:$ww=$ww;$wh=$wh;break; } if($ow<$ww || $oh<$wh)$this->errorfun('背景图片太小!无法生成水印!'); switch($this->wmpos){ case 0:$x=rand(0,($ow-$ww));$y=rand(0,($oh-$wh));break;//随机 case 1:$x=0;$y=0;break;//1为顶端居左 case 2:$x=($ow-$ww)/2;$y=0;break;//2为顶端居中 case 3:$x=$ow-$ww;$y=0;break;//3为顶端居右 case 4:$x=0;$y=($oh-$wh)/2;break;//4为中部居左 case 5:$x=($ow-$ww)/2;$y=($oh-$wh)/2;break;//5为中部居中 case 6:$x=$ow-$ww;$y=($oh-$wh)/2;break;//6为中部居右 case 7:$x=0;$y=$oh-$wh;break;//7为底端居左 case 8:$x=($ow-$ww)/2;$y=$oh-$wh;break;//8为底端居中 case 9:$x=$ow-$ww;$y=$oh-$wh;break;//9为底端居右 default:$x=rand(0,($ow-$ww));$y=rand(0,($oh-$wh));break;//随机 } imagealphablending($g, true); switch($this->wmtype){ case 0: if($this->wmstrcolor){$R=hexdec(substr($this->wmstrcolor,1,2));$G=hexdec(substr($this->wmstrcolor,3,2));$B=hexdec(substr($this->wmstrcolor,5));} else{$this->errorfun('水印文字颜色不存在!');} imagestring($g,$this->wmstrfont,$x,$y,$this->wmstr,imagecolorallocate($g,$R,$G,$B));break; case 1;imagecopymerge($g,$w,$x,$y,0,0,$ww,$wh,$this->wmpictrans);break; default:imagecopymerge($g,$w,$x,$y,0,0,$ww,$wh,$this->wmpictrans);break; } @unlink($this->ulname); switch($groundimg[2]){ case 1:imagegif($g,$this->ulname);break; case 2:imagejpeg($g,$this->ulname,$this->wmpicquality);break; case 3:imagepng($g,$this->ulname);break; case 4:imagewbmp($g,$this->ulname);break; default:$this->errorfun('生成图片失败!'); } if(isset($wmimg))unset($wmimg); if(isset($w))imagedestroy($w); unset($groundimg); imagedestroy($g); } } function errorfun($e='未知错误!'){ $msg='<script language="javascript" type="text/javascript">'; $msg.='alert("'.$e.'");'; $msg.='history.back();'; $msg.='</script>'; echo $msg; exit; } } ?> <form enctype="multipart/form-data" method="post" action="upfile.php"> <input name="upfile" type="file" /> <input type="submit" name="Submit" value="提交" /> php文件 require_once('inc/upload_class.php'); $ul=new upload_class(); $ul->uploadfun();
本博客迁往 http://huangmin.sinaapp.com/ 敬请移步