当前位置:Gxlcms > PHP教程 > php文件上传程序(1/2)_PHP教程

php文件上传程序(1/2)_PHP教程

时间:2021-07-01 10:21:17 帮助过:6人阅读

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

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/631742.htmlTechArticlephp教程文件上传程序 文章提供一款完整理的php文件上传程序实例代码,他可以上传图片并且把图片保存到1:按天存入目录 2:按月存入目录...

人气教程排行