PHP檔案上傳

來源:互聯網
上載者:User
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();

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.