php入门教程单文件上传与多文件上传实例
在php中文件上传我们使会用到$_FILES数组与move_uploaded_file函数,他们可实现文件上传,基于他们两我们就可实例多文件上传了,下面我一一给各位介绍一下.
先来看看$_FILES数组内容如下:
$_FILES['myFile']['name'] 客户端文件的原名称。
$_FILES['myFile']['type'] 文件的 MIME 类型,需要浏览器提供该信息的支持,例如"image/gif"。
$_FILES['myFile']['size'] 已上传文件的大小,单位为字节。
$_FILES['myFile']['tmp_name'] 文件被上传后在服务端储存的临时文件名,一般是系统默认。可以在php.ini的upload_tmp_dir 指定,但用 putenv() 函数设置是不起作用的。
$_FILES['myFile']['error'] 和该文件上传相关的错误代码。['error'] 是在 PHP 4.2.0 版本中增加的。
下面是它的说明:(它们在PHP3.0以后成了常量)
UPLOAD_ERR_OK 值:0; 没有错误发生,文件上传成功。
UPLOAD_ERR_INI_SIZE 值:1; 上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值。
UPLOAD_ERR_FORM_SIZE 值:2; 上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。
UPLOAD_ERR_PARTIAL 值:3; 文件只有部分被上传。
UPLOAD_ERR_NO_FILE 值:4; 没有文件被上传。 值:5; 上传文件大小为0.
注:
1. 文件被上传结束后,默认地被存储在了临时目录中,这时必须将它从临时目录中删除或移动到其它地方,如果没有,则会被删除。也就是不管是否上传成功,脚本执行完后临时目录里的文件肯定会被删除。所以在删除之前要用PHP的 copy() 函数将它复制到其它位置,此时,才算完成了上传文件过程。
2. 在 PHP 4.1.0 版本以前该数组的名称为 $HTTP_POST_FILES,它并不像 $_FILES 一样是自动全局变量。PHP 3 不支持 $HTTP_POST_FILES 数组。
3. 用form上传文件时,一定要加上属性内容 enctype="multipart/form-data",否则用$_FILES[filename]获取文件信息时会报异常。
PHP实例代码如下:
<form enctype= "multipart/form-data" action= "URL" method= "post" > <input name= "myFile" type= "file" > <input type= "submit" value= "上传文件" > </form> function uploadfile( $file ) { $destinationpath = "./upload/" ; if (! file_exists ( $destinationpath )){ mkdir ( $destinationpath , 0777); } //重命名 $filename = date ( 'ymdhis' ) . '_' . iconv( 'utf-8' , 'gb2312' , basename ( $file [ 'name' ])); if (move_uploaded_file( $file [ 'tmp_name' ], $destinationpath . $filename )) { return iconv( 'gb2312' , 'utf-8' , $filename ); } return '' ; }多文件上传的原理也差不多,下面看个实例,代码如下:
< form action = "fileclass.php" enctype = "multipart/form-data" method = "post" id = "userfile" > < table width = "350" border = "0" cellpadding = "0" cellspacing = "5" > < tr > < td > < input name = "userfile[]" type = "file" size = "30" /> </ td > </ tr > < input type = "hidden" name = "MAX_FILE_SIZE" value = "1000000" /> < tr > < td > < input name = "userfile[]" type = "file" size = "30" /> </ td > </ tr > < tr > < td > < input name = "userfile[]" type = "file" size = "30" /> </ td > </ tr > < tr > < td > < input name = "userfile[]" type = "file" size = "30" /> </ td > </ tr > < tr > < td > < input name = "userfile[]" type = "file" size = "30" /> </ td > </ tr > < tr > < td > < input name = "submitfile" type = "submit" value = "确认上传文件" /> < input name = "resetfile" type = "reset" value = "取消上传文件" /> </ td > </ tr > </ table > </ form >fileclass.php文件代码如下:
<?php class more_file_upload{ const FILE_PATH= 'filehandle/uploadfile/' ; var $file_type ; var $file_type_array ; var $file_type_string ; var $file_name ; var $file_size ; var $file_tmp_name ; var $file_error ; var $handledate ; static $totalsize =0; function __construct( $file_name , $file_error , $file_size , $file_tmp_name , $file_type ){ $this ->handledate= date ( 'm-d-Y' ); if (! empty empty ( $file_name )){ $this ->file_name= $file_name ; $this ->file_error= $file_error ; $this ->file_size= $file_size ; $this ->file_tmp_name= $file_tmp_name ; $this ->file_type= $file_type ; $this ->file_type_array= array ( 'text/plain' , 'image/gif' , 'image/jpg' , 'text/html' , 'image/pjpeg' , 'image/png' , 'application/msword' , 'application/pdf' ); $this ->show_execute_message( $this ->file_error, $this ->file_name, $this ->file_type, $this ->file_size); } } function __destruct(){ $this ->file_name = NULL; $this ->file_error = NULL; $this ->file_size = NULL; $this ->file_tmp_name = NULL; $this ->file_type = NULL; self:: $totalsize = 0; } function show_execute_message( $smfileerror , $smfilename , $smfiletype , $smfilesize ){ if ( $smfileerror >0){ switch ( $smfileerror ){ case 1: $smfilemessage = '<strong>文件超过服务器的约定大小!</strong>' ; break ; case 2: $smfilemessage = '<strong>文件超过指定的文件大小!</strong>' ; break ; case 3: $smfilemessage = '<strong>文件只上传了部分!</strong>' ; break ; case 4: echo "$this->file_name " . '文件上传失败!<br/>' ; break ; } self::__destruct(); } else { $smfiletypeflag = array_search ( $smfiletype , $this ->file_type_array); if ( $smfiletypeflag == false){ $smfilemessage = '<strong>文件类型不对,请核实!</strong>' ; self::__destruct(); } else { $resflag = $this ->move_file( $this ->file_tmp_name, $this ->file_name); if ( $resflag == 1){ $smfilemessage = '文件上传成功!' ; self:: $totalsize += intval ( $smfilesize ); self::__destruct(); } else { $smfilemessage = '<strong>文件上传失败!</strong>' ; self::__destruct(); } } } $smfilesize = $smfilesize /1024; $smfilesizeformat =sprintf( '%01d' , $smfilesize ); echo '<tr> <td align= "left" > '.$smfilename.' </td> <td align= "center" > '.$smfiletype.' </td> <td align= "center" > '.$smfilesizeformat.' </td> <td align= "center" > '.$smfilemessage.' </td> </tr>'; } function move_file( $mvfiletmp , $mvfilename ){ //移动文件 $mvfilenamearr = explode ( '.' , basename ( $mvfilename )); $mvtime = mktime (); $mvfilenamearr [0]= $this ->rand_string(10). "$mvtime" ; $mvfilename =implode( '.' , $mvfilenamearr ); if ( is_uploaded_file ( $mvfiletmp )){ $uploadfile =self::FILE_PATH. "$mvfilename" ; $result =move_uploaded_file( $mvfiletmp , $uploadfile ); return $result ; } } function rand_string( $len , $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' ){ //指定范围内随机提取字符 $string = '' ; for ( $i = 0; $i < $len ; $i ++){ $pos = rand(0, strlen ( $chars )-1); $string .= $chars { $pos }; } return $string ; } } echo '<table width="90%" border="1" cellpadding="0" align="center" cellspacing="2">' ; echo '<tr> <td align= "center" >文件名</td> <td align= "center" >文件类型</td> <td align= "center" >文件大小(KB)</td> <td align= "center" >执行结果</td> </tr>'; for ( $i =0; $i < count ( $_FILES [ 'userfile' ]); $i ++){ $filename [ $i ]= $_FILES [ 'userfile' ][ 'name' ][ $i ]; $fileerror [ $i ]= $_FILES [ 'userfile' ][ 'error' ][ $i ]; $filesize [ $i ]= $_FILES [ 'userfile' ][ 'size' ][ $i ]; $filetmpname [ $i ]= $_FILES [ 'userfile' ][ 'tmp_name' ][ $i ]; $filetype [ $i ]= $_FILES [ 'userfile' ][ 'type' ][ $i ]; $filetemp = new more_file_upload( "$filename[$i]" , "$fileerror[$i]" , "$filesize[$i]" , "$filetmpname[$i]" , "$filetype[$i]" ); } echo '</table>' ; echo '<a href="upfile.html">继续上传</a> <a href="index.php">返回首页</a>' ; ?>查看更多关于php入门教程单文件上传与多文件上传实例 - php上的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did29376