PHP上传类-简单入门代码

 1
 2<?php
 3/******************************************************************************
 4
 5参数说明:
 6$max_file_size  : 上传文件大小限制, 单位BYTE
 7$destination_folder : 上传文件路径
 8$watermark   : 是否附加水印(1为加水印,其他为不加水印);
 9
10使用说明:
111. 将PHP.INI文件里面的"extension=php_gd2.dll"一行前面的;号去掉,因为我们要用到GD库;
122. 将extension_dir =改为你的php_gd2.dll所在目录;
13******************************************************************************/
14
15//上传文件类型列表
16$uptypes=array(
17    'image/jpg',
18    'image/jpeg',
19    'image/png',
20    'image/pjpeg',
21    'image/gif',
22    'image/bmp',
23    'image/x-png'
24);
25
26$max_file_size=2000000;     //上传文件大小限制, 单位BYTE
27$destination_folder="uploadimg/"; //上传文件路径
28$watermark=1;      //是否附加水印(1为加水印,其他为不加水印);
29$watertype=1;      //水印类型(1为文字,2为图片)
30$waterposition=1;     //水印位置(1为左下角,2为右下角,3为左上角,4为右上角,5为居中);
31$waterstring="http://www.xplore.cn/";  //水印字符串
32$waterimg="xplore.gif";    //水印图片
33$imgpreview=1;      //是否生成预览图(1为生成,其他为不生成);
34$imgpreviewsize=1/2;    //缩略图比例
35?>
 1<html>
 2<head>
 3<title>ZwelL图片上传程序</title>
 4<style type="text/css">
 5<!--
 6body
 7{
 8     font-size: 9pt;
 9}
10input
11{
12     background-color: #66CCFF;
13     border: 1px inset #CCCCCC;
14}
15-->
16</style>
17</head>
18
19<body>
20<form enctype="multipart/form-data" method="post" name="upform">
21  上传文件:
22  <input name="upfile" type="file">
23  <input type="submit" value="上传"><br>
24  允许上传的文件类型为:<?=implode(', ',$uptypes)?>
25</form>
  1<?php
  2if ($_SERVER['REQUEST_METHOD'] == 'POST')
  3{
  4    if (!is_uploaded_file($_FILES["upfile"][tmp_name]))
  5    //是否存在文件  经过本地测试这里实际上已经把文件上传到了临时文件夹,下面的 $_FILES["upfile"] 会再次上传一次并保存到指定文件夹。相当上传两次这样肯定不合理 刚学习php待解决。
  6    {
  7         echo "图片不存在!";
  8         exit;
  9    }
 10
 11    $file = $_FILES["upfile"];
 12    if($max_file_size < $file["size"])
 13    //检查文件大小
 14    {
 15        echo "文件太大!";
 16        exit;
 17    }
 18
 19    if(!in_array($file["type"], $uptypes))
 20    //检查文件类型
 21    {
 22        echo "文件类型不符!".$file["type"];
 23        exit;
 24    }
 25
 26    if(!file_exists($destination_folder))
 27    {
 28        mkdir($destination_folder);
 29    }
 30
 31    $filename=$file["tmp_name"];
 32    $image_size = getimagesize($filename);
 33    $pinfo=pathinfo($file["name"]);
 34    $ftype=$pinfo['extension'];
 35    $destination = $destination_folder.time().".".$ftype;
 36    if (file_exists($destination) && $overwrite != true)
 37    {
 38        echo "同名文件已经存在了";
 39        exit;
 40    }
 41
 42    if(!move_uploaded_file ($filename, $destination))
 43    {
 44        echo "移动文件出错";
 45        exit;
 46    }
 47
 48    $pinfo=pathinfo($destination);
 49    $fname=$pinfo[basename];
 50    echo " <font color=red>已经成功上传</font><br>文件名:  <font color=blue>".$destination_folder.$fname."</font><br>";
 51    echo " 宽度:".$image_size[0];
 52    echo " 长度:".$image_size[1];
 53    echo "<br> 大小:".$file["size"]." bytes";
 54
 55    if($watermark==1)
 56    {
 57        $iinfo=getimagesize($destination,$iinfo);
 58        $nimage=imagecreatetruecolor($image_size[0],$image_size[1]);
 59        $white=imagecolorallocate($nimage,255,255,255);
 60        $black=imagecolorallocate($nimage,0,0,0);
 61        $red=imagecolorallocate($nimage,255,0,0);
 62        imagefill($nimage,0,0,$white);
 63        switch ($iinfo[2])
 64        {
 65            case 1:
 66            $simage =imagecreatefromgif($destination);
 67            break;
 68            case 2:
 69            $simage =imagecreatefromjpeg($destination);
 70            break;
 71            case 3:
 72            $simage =imagecreatefrompng($destination);
 73            break;
 74            case 6:
 75            $simage =imagecreatefromwbmp($destination);
 76            break;
 77            default:
 78            die("不支持的文件类型");
 79            exit;
 80        }
 81
 82        imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]);
 83        imagefilledrectangle($nimage,1,$image_size[1]-15,80,$image_size[1],$white);
 84
 85        switch($watertype)
 86        {
 87            case 1:   //加水印字符串
 88            imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black);
 89            break;
 90            case 2:   //加水印图片
 91            $simage1 =imagecreatefromgif("xplore.gif");
 92            imagecopy($nimage,$simage1,0,0,0,0,85,15);
 93            imagedestroy($simage1);
 94            break;
 95        }
 96
 97        switch ($iinfo[2])
 98        {
 99            case 1:
100            //imagegif($nimage, $destination);
101            imagejpeg($nimage, $destination);
102            break;
103            case 2:
104            imagejpeg($nimage, $destination);
105            break;
106            case 3:
107            imagepng($nimage, $destination);
108            break;
109            case 6:
110            imagewbmp($nimage, $destination);
111            //imagejpeg($nimage, $destination);
112            break;
113        }
114
115        //覆盖原上传文件
116        imagedestroy($nimage);
117        imagedestroy($simage);
118    }
119
120    if($imgpreview==1)
121    {
122    echo "<br>图片预览:<br>";
123    echo "<img src=\"".$destination."\" width=".($image_size[0]*$imgpreviewsize)." height=".($image_size[1]*$imgpreviewsize);
124    echo " alt=\"图片预览:\r文件名:".$destination."\r上传时间:\">";
125    }
126}
127?>