zoukankan      html  css  js  c++  java
  • php上传图片模范代码

      1 [php] view plaincopyprint?
      2 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
      3   
      4 <?php  
      5 /****************************************************************************** 
      6  
      7 参数说明: 
      8 $max_file_size  : 上传文件大小限制, 单位BYTE 
      9 $destination_folder : 上传文件路径 
     10 $watermark   : 是否附加水印(1为加水印,其他为不加水印); 
     11  
     12 使用说明: 
     13 1. 将PHP.INI文件里面的"extension=php_gd2.dll"一行前面的;号去掉,因为我们要用到GD库; 
     14 2. 将extension_dir =改为你的php_gd2.dll所在目录; 
     15 ******************************************************************************/  
     16   
     17 //上传文件类型列表  
     18 $uptypes=array(  
     19     'image/jpg',  
     20     'image/jpeg',  
     21     'image/png',  
     22     'image/pjpeg',  
     23     'image/gif',  
     24     'image/bmp',  
     25     'image/x-png'  
     26 );  
     27   
     28 $max_file_size=2000000;     //上传文件大小限制, 单位BYTE  
     29 $destination_folder="uploadimg/"; //上传文件路径  
     30 $watermark=1;      //是否附加水印(1为加水印,其他为不加水印);  
     31 $watertype=1;      //水印类型(1为文字,2为图片)  
     32 $waterposition=1;     //水印位置(1为左下角,2为右下角,3为左上角,4为右上角,5为居中);  
     33 $waterstring="http://www.xplore.cn/";  //水印字符串  
     34 $waterimg="xplore.gif";    //水印图片  
     35 $imgpreview=1;      //是否生成预览图(1为生成,其他为不生成);  
     36 $imgpreviewsize=1/2;    //缩略图比例  
     37 ?>  
     38 <html>  
     39 <head>  
     40 <title>ZwelL图片上传程序</title>  
     41 <style type="text/css">  
     42 <!--  
     43 body  
     44 {  
     45      font-size: 9pt;  
     46 }  
     47 input  
     48 {  
     49      background-color: #66CCFF;  
     50      border: 1px inset #CCCCCC;  
     51 }  
     52 -->  
     53 </style>  
     54 </head>  
     55   
     56 <body>  
     57 <form enctype="multipart/form-data" method="post" name="upform">  
     58   上传文件:  
     59   <input name="upfile" type="file">  
     60   <input type="submit" value="上传"><br>  
     61   允许上传的文件类型为:<?=implode(', ',$uptypes)?>  
     62 </form>  
     63   
     64 <?php  
     65 if ($_SERVER['REQUEST_METHOD'] == 'POST')  
     66 {  
     67     if (!is_uploaded_file($_FILES["upfile"][tmp_name]))  
     68     //是否存在文件  
     69     {  
     70          echo "图片不存在!";  
     71          exit;  
     72     }  
     73   
     74     $file = $_FILES["upfile"];  
     75     if($max_file_size < $file["size"])  
     76     //检查文件大小  
     77     {  
     78         echo "文件太大!";  
     79         exit;  
     80     }  
     81   
     82     if(!in_array($file["type"], $uptypes))  
     83     //检查文件类型  
     84     {  
     85         echo "文件类型不符!".$file["type"];  
     86         exit;  
     87     }  
     88   
     89     if(!file_exists($destination_folder))  
     90     {  
     91         mkdir($destination_folder);  
     92     }  
     93   
     94     $filename=$file["tmp_name"];  
     95     $image_size = getimagesize($filename);  
     96     $pinfo=pathinfo($file["name"]);  
     97     $ftype=$pinfo['extension'];  
     98     $destination = $destination_folder.time().".".$ftype;  
     99     if (file_exists($destination) && $overwrite != true)  
    100     {  
    101         echo "同名文件已经存在了";  
    102         exit;  
    103     }  
    104   
    105     if(!move_uploaded_file ($filename, $destination))  
    106     {  
    107         echo "移动文件出错";  
    108         exit;  
    109     }  
    110   
    111     $pinfo=pathinfo($destination);  
    112     $fname=$pinfo[basename];  
    113     echo " <font color=red>已经成功上传</font><br>文件名:  <font color=blue>".$destination_folder.$fname."</font><br>";  
    114     echo " 宽度:".$image_size[0];  
    115     echo " 长度:".$image_size[1];  
    116     echo "<br> 大小:".$file["size"]." bytes";  
    117   
    118     if($watermark==1)  
    119     {  
    120         $iinfo=getimagesize($destination,$iinfo);  
    121         $nimage=imagecreatetruecolor($image_size[0],$image_size[1]);  
    122         $white=imagecolorallocate($nimage,255,255,255);  
    123         $black=imagecolorallocate($nimage,0,0,0);  
    124         $red=imagecolorallocate($nimage,255,0,0);  
    125         imagefill($nimage,0,0,$white);  
    126         switch ($iinfo[2])  
    127         {  
    128             case 1:  
    129             $simage =imagecreatefromgif($destination);  
    130             break;  
    131             case 2:  
    132             $simage =imagecreatefromjpeg($destination);  
    133             break;  
    134             case 3:  
    135             $simage =imagecreatefrompng($destination);  
    136             break;  
    137             case 6:  
    138             $simage =imagecreatefromwbmp($destination);  
    139             break;  
    140             default:  
    141             die("不支持的文件类型");  
    142             exit;  
    143         }  
    144   
    145         imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]);  
    146         imagefilledrectangle($nimage,1,$image_size[1]-15,80,$image_size[1],$white);  
    147   
    148         switch($watertype)  
    149         {  
    150             case 1:   //加水印字符串  
    151             imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black);  
    152             break;  
    153             case 2:   //加水印图片  
    154             $simage1 =imagecreatefromgif("xplore.gif");  
    155             imagecopy($nimage,$simage1,0,0,0,0,85,15);  
    156             imagedestroy($simage1);  
    157             break;  
    158         }  
    159   
    160         switch ($iinfo[2])  
    161         {  
    162             case 1:  
    163             //imagegif($nimage, $destination);  
    164             imagejpeg($nimage, $destination);  
    165             break;  
    166             case 2:  
    167             imagejpeg($nimage, $destination);  
    168             break;  
    169             case 3:  
    170             imagepng($nimage, $destination);  
    171             break;  
    172             case 6:  
    173             imagewbmp($nimage, $destination);  
    174             //imagejpeg($nimage, $destination);  
    175             break;  
    176         }  
    177   
    178         //覆盖原上传文件  
    179         imagedestroy($nimage);  
    180         imagedestroy($simage);  
    181     }  
    182   
    183     if($imgpreview==1)  
    184     {  
    185     echo "<br>图片预览:<br>";  
    186     echo "<img src="".$destination."" width=".($image_size[0]*$imgpreviewsize)." height=".($image_size[1]*$imgpreviewsize);  
    187     echo " alt="图片预览:
    文件名:".$destination."
    上传时间:">";  
    188     }  
    189 }  
    190 ?>  
    191 </body>  
    192 </html>  

    http://blog.csdn.net/xianglunxi/article/details/9359441

  • 相关阅读:
    再次总结Linux最常用命令
    sort命令实战
    JavaScript基础:
    CSS样式基础:
    HTML基础:
    spider(一)
    xgene:疾病相关基因,耳聋,彩色,老年痴呆,帕金森
    xgene:肿瘤相关基因 KRAS,,BRAF,,通路PI3K-AKT
    查询当前Database下所有Datatable及所有记录数
    IIS注册.netframework4.0指令
  • 原文地址:https://www.cnblogs.com/xiaolang1/p/4325353.html
Copyright © 2011-2022 走看看