php上传图片学习笔记与心得
我们在php中上传文件就必须使用#_FILE变量了,这个自动全局变量 $_FILES 从 PHP 4.1.0 版本开始被支持.在这之前,从 4.0.0 版本开始,PHP 支持 $HTTP_POST_FILES 数组.这些数组将包含所有关于您上传的文件的信息,其中,我们推荐您使用 $_FILES.
如果 PHP 的设置选项 register_globals 为 on,则相关的变量名将也会存在.从 PHP 4.2.0 版本开始,register_globals 的默认值被设为 off.
我们假设文件上传字段的名称为 userfile.名称可随意命名.
$_FILES['userfile']['name']
客户端机器文件的原名称.
$_FILES['userfile']['type']
文件的 MIME 类型,需要浏览器提供该信息的支持,例如[image/gif].
$_FILES['userfile']['size']
已上传文件的大小,单位为字节.
$_FILES['userfile']['tmp_name']
文件被上传后在服务端储存的临时文件名.
$_FILES['userfile']['error']
和该文件上传相关的错误代码.['error'] 是在 PHP 4.2.0 版本中增加的.
处理函数:
move_uploaded_file() (PHP 4 >= 4.0.3, PHP 5)
move_uploaded_file -- 将上传的文件移动到新位置
说明
bool move_uploaded_file ( string filename, string destination )
本函数检查并确保由 filename 指定的文件是合法的上传文件(即通过 PHP 的 HTTP POST 上传机制所上传的).如果文件合法,则将其移动为由 destination 指定的文件.
如果 filename 不是合法的上传文件,不会出现任何操作,move_uploaded_file() 将返回 FALSE.如果 filename 是合法的上传文件,但出于某些原因无法移动,不会出现任何操作,move_uploaded_file() 将返回 FALSE.此外还会发出一条警告.
如果目标文件已经存在,将会被覆盖.
实例代码如下:
if (move_uploaded_file( $_FILES [ "magfile" ][ "tmp_name" ], $uploaddir )) { echo "Update OK!" ; }也可用copy(PHP 3, PHP 4, PHP 5)copy -- 拷贝文件bool copy ( string source, string dest )将文件从 source 拷贝到 dest.如果成功则返回 TRUE,失败则返回 FALSE.
提交页 实例代码如下:
<form action= "." method= "post" enctype= "multipart/form-data" name= "UL" > <!--这里的‘enctype= "multipart/form-data" ’是必须的--> <input type= "file" name= "picurl" size= "15" accept= "image/x-png,image/gif,image/jpeg" > <input type= "submit" name= "upload" value= "上传" > </form>处理页 实例代码如下:
if ( $_FILES [ 'picurl' ][ 'size' ] > 0){ if (move_uploaded_file ( $_FILES [ 'picurl' ][ 'tmp_name' ], $_FILES [ 'picurl' ][ 'name' ])){ echo "图片上传成功" ; } }其它非File类型的表单,可照样用$_POST['name']来接收.
php上传图片简单实现实例代码如下:
<html> <head> <title>PHP上传图片简单实现 </title> </head> <body> <?php if ( $_GET [ 'action' ] == 'upfile' ) { $target_path = 'temp_' . $_FILES [ 'photo' ][ 'name' ]; echo '上传的临时文件:' . $_FILES [ 'photo' ][ 'tmp_name' ] . '<br/>' ; echo '上传的目标文件:' . $target_path . '<br/>' ; echo $_SERVER [ "SCRIPT_FILENAME" ] . '<br/>' ; echo $_SERVER [ "OS" ] . '<br/>' ; //测试函数: move_uploaded_file //也可以用函数:copy move_uploaded_file( $_FILES [ 'photo' ][ 'tmp_name' ], $target_path ); echo "Upload result:" ; if ( file_exists ( $target_path )) { if ( $_SERVER [ "OS" ]!= "Windows_NT" ){ @ chmod ( $target_path ,0604); } echo '<font color="green">Succeed!</font><br /><a href="http://' . $_SERVER ["SERVER_NAME "] . " / " .$target_path .'" ><img src=' . $target_path . ' border="0">' ; } else { echo '<font color="red">Failed!</font>' ; } exit ; } ?> <h1>Registration</h1> <form action= "upload.php?action=upfile" method= "post" name= "UForm" enctype= "multipart/form-data" > <fieldset> <legend>Your information</legend> <ul> <li>Your Phot<input type= "file" name= "photo" ></li> </ul> </fieldset> <button type= "submit" >上传</button> </form> </body> </html> 上面代码只适用于学习使用,如果想使用在现在的服务器上我们必须如下写法 实例代码如下: <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 =1; //水印位置(1为左下角,2为右下角,3为左上角,4为右上角,5为居中); $waterstring = "http://www.111cn.net/" ; //水印字符串 $waterimg = "xplore.gif" ; //水印图片 $imgpreview =1; //是否生成预览图(1为生成,其他为不生成); $imgpreviewsize =1/2; //缩略图比例 ?> <html> <head> <title>ZwelL图片上传程序</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' ]; $destination = $destination_folder .time(). "." . $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,80, $image_size [1], $white ); switch ( $watertype ) { case 1: //加水印字符串 imagestring( $nimage ,2,3, $image_size [1]-15, $waterstring , $black ); 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上传时间: ">" ; } } ?> </body> </html>
查看更多关于php上传图片学习笔记与心得 - php上传下载的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did29385