程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php文件上傳程序(1/2)

php文件上傳程序(1/2)

編輯:關於PHP編程

php教程文件上傳程序
文章提供一款完整理的php文件上傳程序實例代碼,他可以上傳圖片並且把圖片保存到1:按天存入目錄 2:按月存入目錄 ,還可以設置上傳圖片生成水印,
*/
?>

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>上傳文件程序</title>
<style type="text/css教程">
*{
 font-size:12px;
 margin:0; padding:0;
}
a:link,a:visited{
 text-decoration:none;
 color: #393
}
a:hover{
 text-decoration:underline;
 color:#0033cc
}
input.text{
 border:1px solid #ccc;height:22px;line-height:22px;padding-left:5px;background:#fff;width:274px;
}
input.button{
 background:#fff url(images/button.png);border:1px solid #9ea9c5;padding:2px 2px 0px 2px;margin-left:4px; margin-right:4px;
}
</style>
<script language=網頁特效>
function check()
{
 var strfilename=document.myform.upfile.value;
 if (strfilename=="")
 {
     alert("請選擇要上傳的文件");
  document.myform.upfile.focus();
     return false;
   }
}
</script>
</head>

<body>
<?php
/***********************
程序:上傳文件
功能:上傳文件、縮略圖、加水印
****************************/
include("common/upfiles.class.php");
$path="../upload/coolsite";    //文件上傳路徑
$mix="smallimg";     //縮略圖路徑(在upload下建立)
$mark="markimg";    //加水引的圖片存放路徑(在upload下建立)
$text = array("www.bKjia.c0m");   //水印內容
$oupload= new upfiles($path,$mix,$mark);   //實例化類文件

if(isset($_post['up'])){
 
 if($_post['urlid']=='1'){ //上傳圖片 參數urlid 1:上傳圖片  2:上傳其他文件..
  
  $oupload->tofile = true;  //開啟則只保存縮略圖或者水印圖,刪除原圖
  $photo = $oupload->upload("upfile"); //上傳的文件域
  $photourl = $oupload->fileurl."/".$photo;
  $newsmallimg = $oupload->smallimg($photo);  //縮略圖功能
  //$newmarkimg = $oupload->watermark($photo,$text); //水印功能

  //echo $newsmallimg;  //輸出縮略圖路徑
  //echo $newmark; //輸出水印圖路徑
  //echo "<img src='".$newsmallimg."' border='0'>"; //輸出縮略圖
  //echo "<img src='".$newmark."' border='0'>"; //輸出水印圖
 }else{
  $upfilename = $oupload->upload("upfile"); //上傳的文件域
 }
 $strjs = "<script language=javascript>n";
 $strjs .= "parent.document.myform.upfile1.value='".$newsmallimg."'n";
 $strjs .= "parent.document.myform.upfile2.value='".$photourl."'n";
 $strjs .= "</script>n";
 echo $strjs;  //把上次文件路徑附在upfile1、upfile2中去
}else{
?>
<form action="upfile.php" method="post" enctype="multipart/form-data" name="myform" onsubmit="return check()">
<input type="file" name="upfile" value="" class="text"><input type="submit" name="up" value="上傳" class="button">
<input type="hidden" name="urlid" value="<?php echo $_get['urlid']?>">
</form>
<?php }?>
</body>
</html>

<?
//upfiles.class.php

/*=========================
上傳類 upfiles.class.php
===========================*/
class upfiles {
  /*=========================
   //基本參數設置
  ===========================*/
        protected $annexfolder = "upload";  //附件存放點,默認為:upload
  protected $dirtype = 2;   //1:按天存入目錄 2:按月存入目錄
        protected $smallfolder = "smallimg";   //縮略圖存放路徑,注:必須是放在 $upload下的子目錄,默認為:smallimg
        protected $markfolder = "markimg";    //水印圖片存放路徑,注:必須是放在 $upload下的子目錄,默認為:markimg
        protected $upfiletype = "jpg gif png rar zip";   //上傳的類型,默認為:jpg gif png rar zip
        protected $upfilemax = 102400;   //上傳大小限制,單位是"kb",默認為:1024kb
        protected $fonttype = "common/equinoxstd.otf";   //水印字體庫
        protected $maxwidth = 800;   //圖片最大寬度
        protected $maxheight = 600;  //圖片最大高度
  
  /*=========================
   //初始化上傳類
  ===========================*/
        public function __construct($annexfolder,$smallfolder,$includefolder) {
   
    switch($this->dirtype)
    {
     case 1: $attach_subdir = 'day_'.date('ymd'); break;
     case 2: $attach_subdir = 'month_'.date('ym'); break;
    }
    $attach_dir = $annexfolder.'/'.$attach_subdir;
    $attach_dir_small = $attach_dir.'/'.$smallfolder;
    $attach_dir_mark = $attach_dir.'/'.$includefolder;    
    
    $this->rootfolder = $annexfolder;
                $this->annexfolder = $attach_dir;
                $this->smallfolder = $attach_dir_small;
    $this->markfolder = $attach_dir_mark;
                //$this->fonttype = $includefolder."/nasaliza.ttf";
        }
  public function __get($fileurl){
   $fileurl = $this->annexfolder;
   return $fileurl;
  }
  /*=========================
   //上傳文件
  ===========================*/
        public function upload($inputname) {
    //檢查文件夾是否存在    
    if(!file_exists($this->annexfolder)){
     if(!file_exists($this->rootfolder)) @mkdir($this->rootfolder);
     if(!file_exists($this->annexfolder)) @mkdir($this->annexfolder);
     if(!file_exists($this->smallfolder)) @mkdir($this->smallfolder);
     if(!file_exists($this->markfolder)) @mkdir($this->markfolder);
    }
    if(!file_exists($this->smallfolder)){
     @mkdir($this->smallfolder);
    }    
    if(!file_exists($this->markfolder)){
     @mkdir($this->markfolder);
    }
        
    $this->uptype = $_files[$inputname]["type"];
    $this->upname = $_files[$inputname]["name"];
    $this->uptmp_name = $_files[$inputname]["tmp_name"];
    $this->ups教程ize = $_files[$inputname]["size"];
    $this->uperror = $_files[$inputname]["error"];

    if($this->uptype){
     switch ($this->uptype)///檢查上傳的類型
     {    
      case "image/pjpeg":    
       $fileextname = "jpg";    
       break;
      case "image/jpeg":    
       $fileextname = "jpg";    
       break;  
      case "image/gif":    
       $fileextname = "gif";    
       break;    
      case "image/x-png":    
       $fileextname = "png";    
       break;    
      case "application/x-shockwave-flash":    
       $fileextname = "swf";    
       break;    
      case "text/plain":    
       $fileextname = "txt";    
       break;    
      case "application/msword":    
       $fileextname = "doc";    
       break;
      case "application/vnd.ms-excel":    
       $fileextname = "xls";    
       break;
      case "application/x-zip-compressed":    
       $fileextname = "zip";    
       break;
      case "audio/mpeg":
       $fileextname = "mp3";    
       break;
      case "audio/x-ms-wma":
       $fileextname = "wma";    
       break;
      case "application/pdf":
       $fileextname = "pdf";    
       break;
      default: //如果不滿足上述類型,那麼上傳文件被判斷為格式不正確!!
       //$fileextname  =strtolower(substr(strrchr(trim($this->upname), "."),1,4));
       //$fileinfo=pathinfo($this->upname);
       //$fileextname=$fileinfo['extension'];
       $fileextname = "err";
      }
     }
    

1 2

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved