php文件上传程序
发布:smiling 来源: PHP粉丝网 添加日期:2013-12-16 17:04:21 浏览: 评论:0
文章提供一款完整理的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=javascript>
- 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.111cn.net"); //水印内容
- $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->upsize = $_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";
- }
- }
- if(@emptyempty($this->upname)) die("没有上传文件信息,请确认 <a href='javascript:history.go(-1);'>返回</a>");
- //$name = explode(".",$this->upname);//将上传前的文件以"."分开取得文件类型
- //$filcount = count($name);//获得截取的数量
- //$filtype = $name[$filcount-1];//取得文件的类型
- $filtype = $fileextname;
- if(strpos($this->upfiletype,$filtype) === false){
- die("上传文件类型仅支持 ".$this->upfiletype." 不支持 ".$filtype ." <a href='javascript:history.go(-1);'>返回</a>");
- }
- $filsize = $this->upsize;
- if(round($filsize/1024) > ($this->upfilemax*1024)) {
- die("上传文件超过 ".$this->upfilemax."kb");
- }
- $filename = "es_".date("ymdhis").".".$filtype;//写入数据库的文件名
- $fileurl = $this->annexfolder."/".$filename;//上传后的文件名称
- $upfileok = move_uploaded_file($this->uptmp_name,$fileurl);
- if($this->uperror == 0 || $upfileok) {
- echo "文件上传成功 <a href='javascript:history.go(-1);'>继续上传</a>";
- } else {
- die("上传文件失败,请确认你的上传文件不超过 $upfilemax kb 或上传时间超时");
- }
- return $filename;
- //return $fileurl;
- }
- public function getinfo($photo) {
- $photo = $this->annexfolder."/".$photo;
- $imageinfo = getimagesize($photo);
- $imginfo["width"] = $imageinfo[0];
- $imginfo["height"] = $imageinfo[1];
- $imginfo["type"] = $imageinfo[2];
- $imginfo["name"] = basename($photo);
- return $imginfo;
- }
- /*=========================
- //缩略图
- ===========================*/
- public function smallimg($photo,$width=250,$height=192) {
- $imginfo = $this->getinfo($photo);
- $photo = $this->annexfolder."/".$photo;//获得图片源
- $newname = substr($imginfo["name"],0,strrpos($imginfo["name"], "."))."_thumb.jpg";//新图片名称
- if($imginfo["type"] == 1) {
- $img = imagecreatefromgif($photo);
- } elseif($imginfo["type"] == 2) {
- $img = imagecreatefromjpeg($photo);
- } elseif($imginfo["type"] == 3) {
- $img = imagecreatefrompng($photo);
- } else {
- $img = "";
- }
- if(emptyempty($img)) return false;
- $width = ($width > $imginfo["width"]) ? $imginfo["width"] : $width;
- $height = ($height > $imginfo["height"]) ? $imginfo["height"] : $height;
- $srcw = $imginfo["width"];
- $srch = $imginfo["height"];
- if ($srcw * $width > $srch * $height) {
- $height = round($srch * $width / $srcw);
- } else {
- $width = round($srcw * $height / $srch);
- }
- if (function_exists("imagecreatetruecolor")) {
- $newimg = imagecreatetruecolor($width, $height);
- imagecopyresampled($newimg, $img, 0, 0, 0, 0, $width, $height, $imginfo["width"], $imginfo["height"]);
- } else {
- $newimg = imagecreate($width, $height);
- imagecopyresized($newimg, $img, 0, 0, 0, 0, $width, $height, $imginfo["width"], $imginfo["height"]);
- }
- if ($this->tofile) {
- if (file_exists($this->smallfolder."/".$newname)){
- @unlink($this->smallfolder."/".$newname);
- }
- imagejpeg($newimg,$this->smallfolder."/".$newname);
- return $this->smallfolder."/".$newname;
- } else {
- imagejpeg($newimg);
- }
- imagedestroy($newimg);
- imagedestroy($img);
- return $newname;
- }
- /*=========================
- //加水印
- ===========================*/
- public function watermark($photo,$text) {
- $imginfo = $this->getinfo($photo);
- $photo = $this->annexfolder."/".$photo;
- $newname = substr($imginfo["name"], 0, strrpos($imginfo["name"], ".")) . "_mark.jpg";
- //$newname = substr($imginfo["name"], 0, strrpos($imginfo["name"], ".")) . ".jpg";
- switch ($imginfo["type"]) {
- case 1:
- $img = imagecreatefromgif($photo);
- break;
- case 2:
- $img = imagecreatefromjpeg($photo);
- break;
- case 3:
- $img = imagecreatefrompng($photo);
- break;
- default:
- return false;
- }
- if (emptyempty($img)) return false;
- $width = ($this->maxwidth > $imginfo["width"]) ? $imginfo["width"] : $this->maxwidth;
- $height = ($this->maxheight > $imginfo["height"]) ? $imginfo["height"] : $this->maxheight;
- $srcw = $imginfo["width"];
- $srch = $imginfo["height"];
- if ($srcw * $width > $srch * $height) {
- $height = round($srch * $width / $srcw);
- } else {
- $width = round($srcw * $height / $srch);
- }
- if (function_exists("imagecreatetruecolor")) {
- $newimg = imagecreatetruecolor($width, $height);
- imagecopyresampled($newimg, $img, 0, 0, 0, 0, $width, $height, $imginfo["width"], $imginfo["height"]);
- } else {
- $newimg = imagecreate($width, $height);
- imagecopyresized($newimg, $img, 0, 0, 0, 0, $width, $height, $imginfo["width"], $imginfo["height"]);
- }
- $white = imagecolorallocate($newimg, 255, 255, 255);
- $black = imagecolorallocate($newimg, 0, 0, 0);
- $alpha = imagecolorallocatealpha($newimg, 230, 230, 230, 80);
- //imagefilledrectangle($newimg, 0, $height-26, $width, $height, $alpha);
- //imagefilledrectangle($newimg, 13, $height-20, 15, $height-7, $black);
- imagettftext($newimg, 14, 0, 20, $height-14, $white, $this->fonttype, $text[0]);
- imagettftext($newimg, 14, 0, 20, $height-6, $white, $this->fonttype, $text[1]);
- if($this->tofile) {
- if (file_exists($this->markfolder."/".$newname)){
- @unlink($this->markfolder."/".$newname);
- }
- imagejpeg($newimg,$this->markfolder."/".$newname);
- if(file_exists($this->annexfolder."/".$newname)){
- unlink($this->annexfolder."/".$newname);
- }
- return $this->markfolder."/".$newname;
- } else {
- imagejpeg($newimg);
- }
- imagedestroy($newimg);
- imagedestroy($img);
- return $newname;
- }
- }?>
Tags: php 文件上传 程序
- 上一篇:php 文件上传实例代码
- 下一篇:php文件上传代码(支持文件批量上传)
相关文章
- ·php实现文件上传的程序代码(2013-11-13)
- ·php 实现多文件上传程序代码(2013-11-13)
- ·php+CKFinder上传中文名文件乱码问题的解决方法(2013-11-14)
- ·php中检测上传文件类型与上传图片大小代码(2013-11-14)
- ·php上传图片失败原图(2013-11-28)
- ·CKEditor+CKFinder+php上传文件配置方法(2013-12-02)
- ·PHP多文件上传(2013-12-09)
- ·php 文件上传实例代码(2013-12-16)
- ·php初学者用文件上传实例(2013-12-23)
- ·php上传文件与图片重命名方法总结(2014-01-06)
- ·如何设置PHP上传文件大小限制(2014-01-06)
- ·PHP文件下载的小实例(2014-01-11)
- ·php获取上传文件名的文件类型(2014-01-15)
- ·多图片上传的php代码(2014-01-15)
- ·php文件上传之php.ini配置上传文件详解(2014-03-02)
- ·PHP文件上传详解(2014-03-02)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)