php文件上传类,支持单个或者多个文件上传
这个文件上传类可以实现多个文件或单个文件进行上传了,下面小编来给各位推荐一个不错的例子,实例代码如下:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://HdhCmsTestw3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd" > <html xmlns= "http://HdhCmsTestphpfensi测试数据/1999/xhtml" > <head> <meta http-equiv= "content-type" content= "text/html; charset=gb2312" /> <title>无标题文档</title> </head> <body> <?php //php文件上传类(该类支持单个或者多个文件上传) /** * 类名:upfile * 作用:处理文件上传 * 说明,该类处理单个或者多个文件上传,使用该类时,只需要实列化该类 * 例: * $up = upfile() * $up->update_file($_file['filename']) * * $up->update_file 函数返回一个数组,如果是多文件上传,则为多维数据。 * 数组的内容: * $fileinfo['file_size'] 上传文件的大小 * $fileinfo['file_suffix'] 上传文件的类型 * $fileinfo['file_name'] 上传文件的名字 * $fileinfo['error'] 上传文件产生的错误 * */ class upfile { public $fcount = 1; //上传文件的数量 public $ftype = array ( 'jpg' , 'jpeg' , 'gif' , 'png' ); //文件格式 public $fsize = 1024; //文件大小单位kb public $fdir = 'HdhCmsTest111cn.net/' ; //文件存放目录 public $errormsg = '' ; //产生的临时错误信息 /** *函数名:get_tmp_file($putfile) *作用:取得上传的临时文件名 *@param array $putfile *@return string $upimg 返回临时文件名 */ function get_tmp_file( $putfile ){ if ( $this ->fcount == 1){ $tmpfile = $putfile [ 'tmp_name' ]; } else { for ( $i =0; $i < $this ->fcount; $i ++){ $tmpfile [] = $putfile [ 'tmp_name' ][ $i ]; } } return $tmpfile ; } /** *函数名:get_suffix($filename) *作用:取得文件的后缀名 *@param file $filename *@return string $suffixname 返回后缀名 */ function get_suffix( $filename ){ $link = pathinfo ( $filename ); $suffixname = strtolower ( $link [ 'extension' ]); return $suffixname ; } /** *验证文件大小 *@author 赵红健 *@param $filesize *@return booln */ function check_file_size( $filesize ){ $this ->errormsg = '' ; if ( $filesize /1000 > $this ->fsize){ $this ->errormsg = '警告:文件超出大小!' ; return false; } else { return true; } } /** *验证文件类型是否合法 *@author 赵红健 *@param $filesuffix *@return booln */ function check_file_suffix( $filesuffix ){ $this ->errormsg = '' ; if (!in_array( $filesuffix , $this ->ftype)){ $this ->errormsg = '警告:文件类型不在允许范围内!' ; return false; } else { return true; } } /** *移动临时文件 *@author 赵红健 *@param $filesuffix *@return booln */ function move_temp_file( $tmpfile , $targetfile ){ $this ->errormsg = '' ; if (!move_uploaded_file( $tmpfile , $targetfile )){ $this ->errormsg = '警告:文件移动失败!' ; return false; } else { return true; } } /** *函数名:update_file($putfile) *作用:上传文件 *@param array $putfile *@return array 文件信息 */ function update_file( $putfile ){ $tmpfile = $this ->get_tmp_file( $putfile ); if (! file_exists ( $this ->fdir)){ $this ->errormsg[] = '错误:目录' . $this ->fdir. '不存在' ; return $this ->errormsg; } $this ->fdir = substr ( $this ->fdir, strlen ( $this ->fdir)-1,1)== '/' ? $this ->fdir: $this ->fdir. '/' ; if (! is_array ( $putfile [ 'size' ])){ $fileinfo [ 'file_size' ] = $putfile [ 'size' ]; if (! $this ->check_file_size( $fileinfo [ 'file_size' ])){ $fileinfo [ 'error' ] = $this ->errormsg; return $fileinfo ; } $fileinfo [ 'file_suffix' ] = $this ->get_suffix( $putfile [ 'name' ]); if (! $this ->check_file_suffix( $fileinfo [ 'file_suffix' ])){ $fileinfo [ 'error' ] = $this ->errormsg; return $fileinfo ; } $fileinfo [ 'file_name' ] = date ( 'ymdhms' ). '.' . $fileinfo [ 'file_suffix' ]; if (! $this ->move_temp_file( $tmpfile , $this ->fdir. $fileinfo [ 'file_name' ])){ $fileinfo [ 'error' ] = $this ->errormsg; return $fileinfo ; } return $fileinfo ; } else { for ( $i =0; $i < $this ->fcount; $i ++){ $fileinfo [ $i ][ 'file_size' ] = $putfile [ 'size' ][ $i ]; if (! $this ->check_file_size( $fileinfo [ $i ][ 'file_size' ])){ $fileinfo [ $i ][ 'error' ] = $this ->errormsg; continue ; } $fileinfo [ $i ][ 'file_suffix' ] = $this ->get_suffix( $putfile [ 'name' ][ $i ]); if (! $this ->check_file_suffix( $fileinfo [ $i ][ 'file_suffix' ])){ $fileinfo [ $i ][ 'error' ] = $this ->errormsg; continue ; } $fileinfo [ $i ][ 'file_name' ] = date ( 'ymdhms' ).rand(). '.' . $fileinfo [ $i ][ 'file_suffix' ]; if (! $this ->move_temp_file( $tmpfile [ $i ], $this ->fdir. $fileinfo [ $i ][ 'file_name' ])){ $fileinfo [ $i ][ 'error' ] = $this ->errormsg; continue ; //开源代码phpfensi测试数据 } } return $fileinfo ; } } } ?> </body> </html>查看更多关于php文件上传类,支持单个或者多个文件上传 - php类的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did29523