三款php多文件上传实例代码
在php开发应用中经常会碰到文件上传,有时也会碰到要多文件上传,下面我们就来看看我提供的三款php多文件上传实例代码,好了费话不说多了来看看这些多文件上传功能适合你么.
示例代码如下:
<form action= "" method= "post" enctype= "multipart/form-data" > <input type= "file" name= "uploadfile[]" > 命名必是这样有 "[]" <input type= "file" name= "uploadfile[]" > //设置允许用户上传的文件类型。 $type = array ( 'gif' , 'jpg' , 'png' , 'zip' , 'rar' ); $upload = new uploadfile( $_files [ 'uploadfile' ], '/' , 1024*1024, $type ); 参数说明:1:表单的文件,2:上传目录,3:支持文件大小,4:允许文件类型 $icount = $upload ->upload(); if ( $icount > 0) { //上传成功 print_r( $upload ->getsaveinfo()); */ class uploadfile { var $postfile = array (); // 用户上传的文件 var $custompath = "" ; // 自定义文件上传路径 var $maxsize = "" ; // 文件最大尺寸 var $lasterror = "" ; // 最后一次出错信息 var $allowtype = array ( 'gif' , 'jpg' , 'png' , 'zip' , 'rar' , 'txt' , 'doc' , 'pdf' ); var $endfilename = "" ; // 最终保存的文件名 var $saveinfo = array (); // 保存文件的最终信息 var $root_dir = "" ; // 项目在硬盘上的位置 /** * 构造函数 * @access public */ function uploadfile( $arrfile , $path = "_" , $size = 2097152, $type = 0) { $this ->postfile = $arrfile ; $this ->custompath = $path == "_" ? "" : $path ; $this ->maxsize = $size ; if ( $type !=0) $this ->allowtype = $arrfile ; $this ->root_dir = $_server [ 'document_root' ]; $this ->_mkdir( $this ->custompath); } /** * 文件上传的核心代码 * @access public * @return int 上传成功文件数 */ function upload() { $ilen = sizeof( $this ->postfile[ 'name' ]); for ( $i =0; $i < $ilen ; $i ++){ if ( $this ->postfile[ 'error' ][ $i ] == 0) { //上传时没有发生错误 //取当前文件名、临时文件名、大小、扩展名,后面将用到。 $sname = $this ->postfile[ 'name' ][ $i ]; $stname = $this ->postfile[ 'tmp_name' ][ $i ]; $isize = $this ->postfile[ 'size' ][ $i ]; $stype = $this ->postfile[ 'type' ][ $i ]; $sextn = $this ->_getextname( $sname ); //开源代码phpfensi.com //检测当前上传文件大小是否合法。 if ( $this ->_checksize){ $this ->lasterror = "您上传的文件[" . $sname . "],超过系统支持大小!" ; $this ->_showmsg( $this ->lasterror); continue ; } if (! is_uploaded_file ( $stname )) { $this ->lasterror = "您的文件不是通过正常途径上传!" ; $this ->_showmsg( $this ->lasterror); continue ; } $_filename = basename ( $sname , "." . $sextn ). "_" .time(). "." . $sextn ; $this ->endfilename = $this ->custompath. $_filename ; if (!move_uploaded_file( $stname , $this ->root_dir. $this ->endfilename)) { $this ->lasterror = $this ->postfile[ 'error' ][ $i ]; $this ->_showmsg( $this ->lasterror); continue ; } //存储当前文件的有关信息,以便其它程序调用。 $this ->save_info[] = array ( "name" => $sname , "type" => $sextn , "size" => $isize , "path" => $this ->endfilename); } } return sizeof( $this ->save_info); }
/** * 得到上传信息 * @access public * @return array 保存信息 */ function getsaveinfo(){ return $this ->save_info; } /** * 得到文件名的扩展名 * @access private * @return string 返回文件扩展名 */ private function _getextname( $filename ){ $arrparts = pathinfo ( $filename ); return $arrparts [ 'extension' ]; } /** * 判断文件大小 * @access private * @return boolean 传入size大于系统定义,则true,反之false */ private function _checksize( $size ){ return $size > $this ->maxsize; } /** * 输出错误信息 * @access private * @return void */ private function _showmsg( $msg ){ printf( "<b><错误信息:></b> %s <br>n" , $msg ); } /** * 新增多级目录 * @access private * @return void */ private function _mkdir( $p ){ $ap = split( '[/]' , $p ); foreach ( $ap as $v ){ if (! empty empty ( $v )){ if ( empty empty ( $path )) $path = $v ; else $path .= '/' . $v ; file_exists ( $this ->root_dir. "/" . $path ) or mkdir ( $this ->root_dir. "/" . $path ); } } } } /* } /**/ /* //注意,上传组件name属性不管是一个还是多个都要使用数组形式,如: <input type="file" name="user_upload_file[]"> <input type="file" name="user_upload_file[]"> //如果用户点击了上传按钮。 if ($_post['action'] == "上传") { //设置允许用户上传的文件类型。 $type = array('gif', 'jpg', 'png', 'zip', 'rar'); //实例化上传类,第一个参数为用户上传的文件组、第二个参数为存储路径、 //第三个参数为文件最大大小。如果不填则默认为2m //第四个参数为充许用户上传的类型数组。如果不填则默认为gif, jpg, png, zip, rar, txt, doc, pdf $upload = new uploadfile($_files['user_upload_file'], 'j:/tmp', 100000, $type); //上传用户文件,返回int值,为上传成功的文件个数。 $num = $upload->upload(); if ($num != 0) { echo "上传成功<br>"; //取得文件的有关信息,文件名、类型、大小、路径。用print_r()打印出来。 print_r($upload->getsaveinfo()); echo $num."个文件上传成功"; } else { echo "上传失败<br>"; } } * @(#)uploadfile.php * * 可同时处理用户多个上传文件。效验文件有效性后存储至指定目录。 * 可返回上传文件的相关有用信息供其它程序使用。(如文件名、类型、大小、保存路径) * 使用方法请见本类底部(uploadfile类使用注释)信息。 * */ class uploadfile { var $user_post_file = array (); //用户上传的文件 var $save_file_path ; //存放用户上传文件的路径 var $max_file_size ; //文件最大尺寸 var $last_error ; //记录最后一次出错信息 //默认允许用户上传的文件类型 var $allow_type = array ( 'gif' , 'jpg' , 'png' , 'zip' , 'rar' , 'txt' , 'doc' , 'pdf' ); var $final_file_path ; //最终保存的文件名 var $save_info = array (); //返回一组有用信息,用于提示用户。 /**/ /** * 构造函数,用与初始化相关信息,用户待上传文件、存储路径等 * * @param array $file 用户上传的文件 * @param string $path 存储用户上传文件的路径 * @param integer $size 允许用户上传文件的大小(字节) * @param array $type 此数组中存放允计用户上传的文件类型 */ function uploadfile( $file , $path , $size = 2097152, $type = '' ) { $this ->user_post_file = $file ; $this ->save_file_path = $path ; $this ->max_file_size = $size ; //如果用户不填写文件大小,则默认为2m. if ( $type != '' ) $this ->allow_type = $type ; } /**/ /** * 存储用户上传文件,检验合法性通过后,存储至指定位置。 * @access public * @return int 值为0时上传失败,非0表示上传成功的个数。 */ function upload() { for ( $i = 0; $i < count ( $this ->user_post_file[ 'name' ]); $i ++) { //如果当前文件上传功能,则执行下一步。 if ( $this ->user_post_file[ 'error' ][ $i ] == 0) { //取当前文件名、临时文件名、大小、扩展名,后面将用到。 $name = $this ->user_post_file[ 'name' ][ $i ]; $tmpname = $this ->user_post_file[ 'tmp_name' ][ $i ]; $size = $this ->user_post_file[ 'size' ][ $i ]; $mime_type = $this ->user_post_file[ 'type' ][ $i ]; $type = $this ->getfileext( $this ->user_post_file[ 'name' ][ $i ]); //检测当前上传文件大小是否合法。 if (! $this ->checksize( $size )) { $this ->last_error = "the file size is too big. file name is: " . $name ; $this ->halt( $this ->last_error); continue ; } //检测当前上传文件扩展名是否合法。 if (! $this ->checktype( $type )) { $this ->last_error = "unallowable file type: ." . $type . " file name is: " . $name ; $this ->halt( $this ->last_error); continue ; } //检测当前上传文件是否非法提交。 if (! is_uploaded_file ( $tmpname )) { $this ->last_error = "invalid post file method. file name is: " . $name ; $this ->halt( $this ->last_error); continue ; } //移动文件后,重命名文件用。 $basename = $this ->getbasename( $name , "." . $type ); //移动后的文件名 $saveas = $basename . "-" .time(). "." . $type ; //组合新文件名再存到指定目录下,格式:存储路径 + 文件名 + 时间 + 扩展名 $this ->final_file_path = $this ->save_file_path. "/" . $saveas ; if (!move_uploaded_file( $tmpname , $this ->final_file_path)) { $this ->last_error = $this ->user_post_file[ 'error' ][ $i ]; $this ->halt( $this ->last_error); continue ; } //存储当前文件的有关信息,以便其它程序调用。 $this ->save_info[] = array ( "name" => $name , "type" => $type , "mime_type" => $mime_type , "size" => $size , "saveas" => $saveas , "path" => $this ->final_file_path); } } return count ( $this ->save_info); //返回上传成功的文件数目 } /**/ /** * 返回一些有用的信息,以便用于其它地方。 * @access public * @return array 返回最终保存的路径 */ function getsaveinfo() { return $this ->save_info; } /**/ /** * 检测用户提交文件大小是否合法 * @param integer $size 用户上传文件的大小 * @access private * @return boolean 如果为true说明大小合法,反之不合法 */ function checksize( $size ) { if ( $size > $this ->max_file_size) { return false; } else { return true; } } /**/ /** * 检测用户提交文件类型是否合法 * @access private * @return boolean 如果为true说明类型合法,反之不合法 */ function checktype( $extension ) { foreach ( $this ->allow_type as $type ) { if ( strcasecmp ( $extension , $type ) == 0) return true; } return false; } /**/ /** * 显示出错信息 * @param $msg 要显示的出错信息 * @access private */ function halt( $msg ) { printf( "<b><uploadfile error:></b> %s <br>n" , $msg ); } /**/ /** * 取文件扩展名 * @param string $filename 给定要取扩展名的文件 * @access private * @return string 返回给定文件扩展名 */ function getfileext( $filename ) { $stuff = pathinfo ( $filename ); return $stuff [ 'extension' ]; } /**/ /** * 取给定文件文件名,不包括扩展名。 * eg: getbasename("j:/hexuzhong.jpg"); //返回 hexuzhong * * @param string $filename 给定要取文件名的文件 * @access private * @return string 返回文件名 */ function getbasename( $filename , $type ) { $basename = basename ( $filename , $type ); return $basename ; } } /**/ /******************** uploadfile类使用注释 */ ?>一个简单实例,代码如下:
<form enctype= "multipart/form-data" action= "up.php" method= "post" > send this file: <input name= "userfile[]" type= "file" /><br> send this file: <input name= "userfile[]" type= "file" /><br> send this file: <input name= "userfile[]" type= "file" /><br> <input type= "submit" value= "send file" /> </form> <?php // in php versions earlier than 4.1.0, $http_post_files should be used instead // of $_files. $uploaddir = './' ; /* print "<pre>"; if (move_uploaded_file($_files['userfile']['tmp_name'], $uploadfile)) { print "file is valid, and was successfully uploaded. "; print "here's some more debugging info:n"; print_r($_files); } else { print "possible file upload attack! here's some debugging info:n"; print_r($_files); } print "</pre>"; */ ?> <?php function rearrayfiles(& $file_post ) { $file_ary = array (); $file_count = count ( $file_post [ 'name' ]); $file_keys = array_keys ( $file_post ); for ( $i =0; $i < $file_count ; $i ++) { foreach ( $file_keys as $key ) { $file_ary [ $i ][ $key ] = $file_post [ $key ][ $i ]; } } return $file_ary ; } print "<pre>" ; if ( $_files [ 'userfile' ]) { $file_ary = rearrayfiles( $_files [ 'userfile' ]);查看更多关于三款php多文件上传实例代码 - php上传下载的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did29308