php檔案上傳程式(1/2)_PHP教程

來源:互聯網
上載者:User
php教程檔案上傳程式
文章提供一款完整理的php檔案上傳程式執行個體代碼,他可以上傳圖片並且把圖片儲存到1:按天存入目錄 2:按月存入目錄 ,還可以設定上傳圖片產生浮水印,
*/
?>





上傳檔案程式



/***********************
程式:上傳檔案
功能:上傳檔案、縮圖、加浮水印
****************************/
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 ""; //輸出縮圖
//echo ""; //輸出浮水印圖
}else{
$upfilename = $oupload->upload("upfile"); //上傳的檔案域
}
$strjs = "n";
echo $strjs; //把上次檔案路徑附在upfile1、upfile2中去
}else{
?>



//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

http://www.bkjia.com/PHPjc/631742.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/631742.htmlTechArticlephp教程檔案上傳程式 文章提供一款完整理的php檔案上傳程式執行個體代碼,他可以上傳圖片並且把圖片儲存到1:按天存入目錄 2:按月存入目錄...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.