PHP 实现图片上传的代码(带返回值)
内容摘要
这篇文章主要为大家详细介绍了PHP 实现图片上传的代码(带返回值),具有一定的参考价值,可以用来参考一下。
功能:上传图片,并自动关闭自己,把图片的地址返回给上一级表单
<meta
功能:上传图片,并自动关闭自己,把图片的地址返回给上一级表单
<meta
文章正文
这篇文章主要为大家详细介绍了PHP 实现图片上传的代码(带返回值),具有一定的参考价值,可以用来参考一下。
功能:上传图片,并自动关闭自己,把图片的地址返回给上一级表单1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | <code class = "php" > <meta http-equiv= "Content-Type" content= "text/html; charset=utf-8" /> <?php /****************************************************************************** 参数说明: $max_file_size : 上传文件大小限制, 单位BYTE $destination_folder : 上传文件路径 $watermark : 是否附加水印(1为加水印,其他为不加水印); 使用说明: 1. 将PHP.INI文件里面的"extension=php_gd2.dll"一行前面的;号去掉,因为我们要用到GD库; 2. 将extension_dir =改为你的php_gd2.dll所在目录; ******************************************************************************/ //上传文件类型列表 $uptypes = array ( 'image/jpg' , 'image/jpeg' , 'image/png' , 'image/pjpeg' , 'image/gif' , 'image/bmp' , 'image/x-png' ); $max_file_size =2000000; //上传文件大小限制, 单位BYTE $destination_folder = "../uploadimg/" ; //上传文件路径 $watermark =1; //是否附加水印(1为加水印,其他为不加水印); $watertype =1; //水印类型(1为文字,2为图片) $waterposition = 2; //水印位置(1为左下角,2为右下角,3为左上角,4为右上角,5为居中); $waterstring = "http://WWW.512pic.COM" ; //水印字符串 $waterimg = "xplore.gif" ; //水印图片 $imgpreview =1; //是否生成预览图(1为生成,其他为不生成); $imgpreviewsize =1; //缩略图比例 1/2 $destination_folder = $destination_folder . date ( 'Y' ,time()). "/" ; if (! file_exists ( $destination_folder )) //检查年份文件是否存在,不存在则创建 { //检查是否有该文件夹,如果没有就创建,并给予最高权限 mkdir ( "$destination_folder" , 0700); } //END IF $destination_folder = $destination_folder . date ( 'md' ,time()). "/" ; if (! file_exists ( $destination_folder )) //检查年份文件是否存在,不存在则创建 { //检查是否有该文件夹,如果没有就创建,并给予最高权限 mkdir ( "$destination_folder" , 0700); } //END IF ?> <html> <head> <title>图片上传程序</title> <style type= "text/css" > <!-- body { font-size: 9pt; } input { background-color: #66CCFF; border: 1px inset #CCCCCC; } --> </style> </head> <body> <form enctype= "multipart/form-data" method= "post" name= "upform" > 上传文件: <input name= "upfile" type= "file" > <input type= "submit" value= "上传" ><br> 允许上传的文件类型为:<?=implode( ', ' , $uptypes )?> </form> <?php if ( $_SERVER [ 'REQUEST_METHOD' ] == 'POST' ) { if (! is_uploaded_file ( $_FILES [ "upfile" ][ "tmp_name" ])) //是否存在文件 { echo "图片不存在!" ; exit ; } $file = $_FILES [ "upfile" ]; if ( $max_file_size < $file [ "size" ]) //检查文件大小 { echo "文件太大!" ; exit ; } if (!in_array( $file [ "type" ], $uptypes )) //检查文件类型 { echo "文件类型不符!" . $file [ "type" ]; exit ; } if (! file_exists ( $destination_folder )) { mkdir ( $destination_folder ); } $filename = $file [ "tmp_name" ]; //文件名 $image_size = getimagesize ( $filename ); //文件大小 $pinfo = pathinfo ( $file [ "name" ]); $ftype = $pinfo [ 'extension' ]; //文件类型 date_default_timezone_set( 'PRC' ); $fnameurl = date ( "YmdHis" ).rand(100000,999999); //文件名 年月日时分秒+6位随机数 $destination = $destination_folder . $fnameurl . "." . $ftype ; //文件路径 if ( file_exists ( $destination ) && $overwrite != true) { echo "同名文件已经存在了" ; exit ; } if (!move_uploaded_file ( $filename , $destination )) { echo "移动文件出错" ; exit ; } $pinfo = pathinfo ( $destination ); $fname = $pinfo [ "basename" ]; echo " <font color=red>已经成功上传</font><br>文件名: <font color=blue>" . $destination_folder . $fname . "</font><br>" ; echo " 宽度:" . $image_size [0]; echo " 长度:" . $image_size [1]; echo "<br> 大小:" . $file [ "size" ]. " bytes" ; if ( $watermark ==1) { $iinfo = getimagesize ( $destination , $iinfo ); $nimage =imagecreatetruecolor( $image_size [0], $image_size [1]); $white =imagecolorallocate( $nimage ,255,255,255); $black = imagecolorallocate( $nimage ,0,0,0); $red =imagecolorallocate( $nimage ,255,0,0); imagefill( $nimage ,0,0, $white ); switch ( $iinfo [2]) { case 1: $simage =imagecreatefromgif( $destination ); break ; case 2: $simage =imagecreatefromjpeg( $destination ); break ; case 3: $simage =imagecreatefrompng( $destination ); break ; case 6: $simage =imagecreatefromwbmp( $destination ); break ; default : die ( "不支持的文件类型" ); exit ; } imagecopy( $nimage , $simage ,0,0,0,0, $image_size [0], $image_size [1]); imagefilledrectangle( $nimage ,1, $image_size [1]-15,125, $image_size [1], $black ); switch ( $watertype ) { case 1: //加水印字符串 imagestring( $nimage ,2,3, $image_size [1]-15, $waterstring , $red ); //图象地址,使用内置字体,X坐标,Y坐标,字符串,颜色 break ; case 2: //加水印图片 $simage1 =imagecreatefromgif( "xplore.gif" ); imagecopy( $nimage , $simage1 ,0,0,0,0,85,15); imagedestroy( $simage1 ); break ; } switch ( $iinfo [2]) { case 1: //imagegif($nimage, $destination); imagejpeg( $nimage , $destination ); break ; case 2: imagejpeg( $nimage , $destination ); break ; case 3: imagepng( $nimage , $destination ); break ; case 6: imagewbmp( $nimage , $destination ); //imagejpeg($nimage, $destination); break ; } //覆盖原上传文件 imagedestroy( $nimage ); imagedestroy( $simage ); } if ( $imgpreview ==1) { echo "<br>图片预览:<br>" ; echo "<img src=\"" . $destination . "\" width=" .( $image_size [0]* $imgpreviewsize ). " height=" .( $image_size [1]* $imgpreviewsize ); echo " alt=\"图片预览:\r文件名:" . $destination . "\r上传时间:\">" ; $uu = str_replace ( ".." , "" , $destination_folder . $fname ); //替换前面冒号 echo "<script language=javascript>window.opener.document.form.File_img.value='" . $uu ."'; var opened=window.open( 'about:blank' , '_self' ); opened.opener=null; opened.close(); </script>"; //将路径返回表单form中的文本框file //关闭自己 } } ?> </body> </html> /*** 以上代码来自:php教程(www.idcnote.com) ***/ </code> |
注:关于PHP 实现图片上传的代码(带返回值)的内容就先介绍到这里,更多相关文章的可以留意
代码注释