文章提供一款完整理的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";
- $mark="markimg";
- $text = array("www.111cn.net");
- $oupload= new upfiles($path,$mix,$mark);
- if(isset($_post['up'])){
-
- if($_post['urlid']=='1'){
-
- $oupload->tofile = true;
- $photo = $oupload->upload("upfile");
- $photourl = $oupload->fileurl."/".$photo;
- $newsmallimg = $oupload->smallimg($photo);
-
-
-
-
-
- }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;
- }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>
- <?
-
-
-
-
- class upfiles {
-
-
-
- protected $annexfolder = "upload";
- protected $dirtype = 2;
- protected $smallfolder = "smallimg";
- protected $markfolder = "markimg";
- protected $upfiletype = "jpg gif png rar zip";
- protected $upfilemax = 102400;
- 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;
-
- }
- 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 = "err";
- }
- }
- if(@emptyempty($this->upname)) die("没有上传文件信息,请确认 <a href='javascript:history.go(-1);'>返回</a>");
-
-
-
-
- $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;
-
- }
- 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";
-
- 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);
-
-
- 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;
- }
- }?>
(责任编辑:admin) |