当前位置: 首页 > 图文教程 > 网络编程 > PHP > 专为新手写的结合smarty的类

PHP
php 多线程上下文中安全写文件实现代码
PHP类的使用 实例代码讲解
用php实现让页面只能被百度gogole蜘蛛访问的方法
php 学习笔记
PHP编程过程中需要了解的this,self,parent的区别
php 操作excel文件的方法小结
使用PHP获取网络文件的实现代码
PHP 巧用数组降低程序的时间复杂度
php下将XML转换为数组
php 文件上传代码(限制jpg文件)
php 无极分类(递归)实现代码
PHP 采集获取指定网址的内容
PHP 将图片按创建时间进行分类存储的实现代码
PHP 存储文本换行实现方法
PHP 批量更新网页内容实现代码
用PHP查询搜索引擎排名位置的代码
用php实现的获取网页中的图片并保存到本地的代码
php实现首页链接查询 友情链接检查的代码
处理php自动反斜杠的函数代码
php实现的遍历文件夹下所有文件,编辑删除

PHP 中的 专为新手写的结合smarty的类


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 113 ::
收藏到网摘: n/a

专为新手写的结合smarty的类,诚挚邀请大家多提宝贵意见

这个是为新手(也为自己)写的结合smarty一起使用的类,是未完成的,现在放出来的目的不是马上让新手使用,所以也没有把注解写的非常详细
希望各位高手多多提意见,我尽量完善它。
首先声明,我写这个的目的也是为了锻炼自己,尽管我知道现在已经有很多类似的类了,但是我还是决定来写一个,
所以请大家在浏览的时候口下留情。
还有文件我也已经打包上传了,放在下面,请大家多多下载,多多提意见。有什么问题直接Q我
目前该类包括以下功能(使用范例,设$m = new Machine_m())
[数据库]
目前支持MYSQL与ACCESS两种数据库
配置参考config.php文件
使用:$m->send_query(SQL语句)//
$m->select_query(SQL语句,是否返回资源,默认为false即返回一个二维数组)
[错误处理]
分为系统错误和用户错误
系统错误:
$this->sys_err( '配置错误,请检查config配置文件', 'die');
第一个参数将记录错误信息到/lib/error/system.err中,第二个参数是处理方法(keep or die),如果需要修改浏览器端提示的话,还可以设置第三个参数,它默认为“很抱歉,本站发生系统错误,请稍候再试。”
用户错误:
$m->user_err( '注册系统关闭', 'die', $_SERVER['HTTP_REFERER'] );
第一个参数是显示到浏览器端的提示,第二个参数是处理方法(keep or die),第三个参数是跳转页面,如果需要记录错误信息的话,还可以设置第四个参数,它将记录错误信息
到/lib/error/user.err中,不设默认不保存。
浏览器端的错误提示默认调用/lib/error/下的err_page.htm模版文件,也可以设置自己的错误模版文件,然后用$m->err_page=加载。
[静态生成]
只用短短一行就自动生成静态页面,跳转时可设置静态页面过期时间
(现在还是不完全的静态,完全的比较复杂,目前没有整合,大家如果要实现完全静态的话,可以结合我的create_html函数和文本操作系列函数实现)
使用方法:
$m->create_html(模版文件,静态输出路径,输出的文件名);
跳转:
$m->goto_html();
输出文件名默认等于当前php文件的文件名,提供这个参数目的是,当有需要使用静态分页时,可以用这个参数设置
[二维数组排序 (推荐)]
可以让二维数组作类似:“先按字段a升序,再按字段b降序”这样的排序
使用方法:
设有这样一个数组:$x = array( array('name'=>'machine_马', 'age'=>23),array('name'=>'tom',age=>28),…… )
我们现在要把此此数组先按name升序排,再按age降序排
则用法为m_sort($x,'name',SORT_ASC,'age',SORT_DESC)
[动态加载]
对于不常用的功能,我采用加载的方法来使用,个人认为这样可以节约资源
例如,如果我们要使用m_sort函数时,默认是没有加载这个函数的
需要这样加载:$m->load_func('m_sort')
然后就可以使用m_sort函数了
[分页]
这里我也不知道做得好不好,我是先写了一个类,再写一个函数来调它,目的是使用得时候比较方便
使用方法:m_page(数据条数,当前页号,每页多少行,显示多少个跳转链接)
函数返回一个数组:array(
'rows' => 每页显示多少行,
'prve' => 上一大页页号,//所谓大页,就是类似上7页,下7页这样的跳转
'next' => 下一大页页号,
'pages' => 共有多少页,
'start' => SQL查询的开始记录数,
'count' => 共有多少条记录,
'links' => 链接页号,//如果共有13页,链接数是7个,当前又在第二大页,则输出array(8,9,10,11,12,13)
'current_page' => 当前页号
);
[验证表单]
把需要验证的表单事先写进函数类,判别的时候只需要把$_POST传入就可以了
使用方法:这个大家自己看函数就应该看得明白,这个函数需要大家按照自己的需求来修改的
[防止跨站攻击]
把这个功能也写道一个函数内了
[中文截取函数]
不是我写的,拿来修改了一下而已
[上传文件]
m_up_file($_FILES,上传路径,文件类型,大小限制)
其中上传路径可以这样设置,1:直接写文件夹路径,2:array('gif'=>'file/gif','jpg'=>'file=>jpg'),这样gif文件自动放入file/gif文件夹,jpg文件放入file/jpg文件夹
文件类型:写法1:'jpg',写法2:array('jpg','jpeg','gif')
返回array( 'arr' => 已上传的文件数组, 'err_msg' => 上传过程中的错误信息, 'num' => 上传成功数 )
[文本操作 (推荐)]
假设有这样一个字符串 $str="你好<!--content-->phpchina<!--/content-->";
我们可以这样修改 $new_str=m_txt_replace('content','machine_马',$str);
现在$new_str的值为"你好<!--content-->machine_马<!--/content-->"
其他的几个函数,如:m_txt_add,m_txt_delete,m_txt_get都是类似的,大家可以自行参看。
注意:这个就是生成静态页面之后,修改的方法。
可以参见6to23并思考为什么他一篇帖子放了那么多回帖速度还那么快
答:因为它的回帖没有进数据库而是直接写到静态文件里面,再使用类似我上面的方法来修改的。你可以看他的源代码,找找<!--你就知道了。

目前已经基本实现就是以上功能了
接下来还打算做
[图片处理]
[UBB代码输出]
等一些其他常用功能
希望各位高手多多提意见
主类:

复制代码 代码如下:

<?php
session_cache_limiter( 'private,must-revalidate');
session_start();
if ( !defined( 'THIS_DIR' ) )
{
die( '系统错误:路径常量丢失' );
}
if ( @!include_once( THIS_DIR . 'lib/smarty/Smarty.class.php' ) )
{
die( '系统错误:smarty文件丢失' );
}
unset( $GLOBALS );
unset( $HTTP_COOKIE_VARS );
unset( $HTTP_ENV_VARS );
unset( $HTTP_GET_VARS );
unset( $HTTP_POST_FILES );
unset( $HTTP_POST_VARS );
unset( $HTTP_SERVER_VARS );
unset( $HTTP_SESSION_VARS );
class Machine_m extends Smarty
{
// 数据库资源
private $conn;
// 路径
private $the_dir;
// 配置文件
private $config = array();
private $config_url;
// 外部函数列表
private $func_list = array();
private $func_list_url;
// 错误提示页面
public $err_page = 'lib/error/err_page.htm';
// 静态生成
public $html_cache = 'html';
public $html_cache_lifetime = 86400;
// 构造函数
public function __construct( $test = false )
{
// 保留smarty类的构造部分
$this->assign('SCRIPT_NAME', isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME']
: @$GLOBALS['HTTP_SERVER_VARS']['SCRIPT_NAME']);
// 现在是machine_m的构造部分
$this->left_delimiter = '<%';
$this->right_delimiter = '%>';
$this->the_dir = THIS_DIR;
$this->config_url = "{$this->the_dir}lib/config/config.php";
$this->config = $this->parse_ini();
$this->func_list_url = "{$this->the_dir}lib/config/func_list.php";
$this->func_list = $this->parse_func();
$this->state( $test );
$this->load_func( array( 'm_addslashes', 'm_stripslashes' ) );
$this->connect();
}
// 析构函数
public function __destruct()
{
}
// 设置网站状态函数
private function state( $test )
{
if ( $test == true)
{
$this->load_func( array( 'm_print_r', 'm_var_dump' ) );
$this->compile_check = true;
error_reporting(E_ALL);
}
else
{
$this->compile_check = false;
error_reporting(0);
}
}
// 解析配置文件函数
private function parse_ini()
{
if ( !file_exists( $this->config_url ) )
{
$this->sys_err( "config配置文件丢失", 'die' );
}
$config = parse_ini_file( $this->config_url );
if ( !array_key_exists( 'host_name', $config )
|| !array_key_exists( 'user_name', $config )
|| !array_key_exists( 'db_name', $config )
|| !array_key_exists( 'password', $config ) )
{
$this->sys_err( '配置错误,请检查config配置文件', 'die');
}
$config = $this->decode_config( $config );
settype( $config, 'object');
return $config;
}
// 解析函数列表函数
private function parse_func()
{
if ( !file_exists( $this->func_list_url ) )
{
$this->sys_err( "func_list配置文件丢失", 'die' );
}
$func_list = parse_ini_file( $this->func_list_url );
return $func_list;
}
// 外部函数加载函数
public function load_func( $func )
{
if ( is_array( $func ) )
{
foreach ( $func as $func_name )
{
$this->include_file( $this->func_list[$func_name] );
}
}
else
{
$this->include_file( $this->func_list[$func] );
}
}
// 外部函数包含函数
public function include_file( $file_url )
{
$file_url = $this->the_dir .$file_url;
@$ok = include_once( $file_url );
if ( $ok != true )
{
$this->sys_err( "文件{{$file_url}}加载失败", 'die' );
}
}
// 对config文件解码函数(将数据库用户名和密码明文纪录是不安全的,最好先加密,再在此解密,本函数可以重载)
protected function decode_config( $config )
{
return $config;
}
// 连接数据库函数
private function connect()
{
switch ( strtoupper( $this->config->database ) )
{
case 'MYSQL' :
$this->connect_mysql();
break;
case 'ACCESS' :
$this->connect_access();
break;
default :
$this->sys_err( '数据库类型错误,该类目前只支持MYSQL与ACCESS两种数据库', 'die');
break;
}
}
// 连接MYSQL数据库函数
private function connect_mysql()
{
if ( $this->conn != null )
{
@mysql_close( $this->conn );
$this->conn = null;
}
@$this->conn = mysql_connect( $this->config->host_name, $this->config->user_name, $this->config->password );
if ( $this->conn == false )
{
$mysql_err = mysql_error();
$this->sys_err( "MYSQL数据库连接失败,原因是:{{$mysql_err}}", 'die' );
}
@$db = mysql_select_db( $this->config->db_name, $this->conn ) ;
if ( $db == false )
{
$mysql_err = mysql_error();
$this->sys_err( "数据表连接失败,原因是:{{$mysql_err}}", 'die' );
}
}
// 连接ACCESS数据库函数
private function connect_access()
{
if ( $this->conn != null )
{
@odbc_close( $this->conn );
$this->conn = null;
}
$dsn = 'Driver=Microsoft Access Driver (*.mdb);dbq=' . realpath( $this->the_dir . $this->config->db_name );
@$this->conn = odbc_connect( $dsn, $this->config->user_name, $this->config->password );
if ( $this->conn == false )
{
@$odbc_err = odbc_errormsg( $this->conn );
$this->sys_err( "ACCESS数据库连接失败,原因是:{{$odbc_err}}", 'die' );
}
}

复制代码 代码如下:

// 发送SQL语句函数
public function send_query( $sql )
{
switch ( strtoupper( $this->config->database ) )
{
case 'MYSQL' :
return $this->send_mysql_query( $sql );
break;
case 'ACCESS' :
return $this->send_odbc_query( $sql );
break;
default :
$this->sys_err( '数据库类型错误,该类目前只支持MYSQL与ACCESS两种数据库', 'die' );
break;
}
}
// 发送SQL语句到MYSQL函数
private function send_mysql_query( $sql )
{
@$rs = mysql_query( $sql, $this->conn );
if ( $rs == false )
{
$mysql_err = mysql_error();
$this->sys_err( "SQL语句:{{$sql}}执行失败,原因是:{{$mysql_err}}", 'die' );
}
return $rs;
}
// 发送SQL语句到ACCESS函数
private function send_odbc_query( $sql )
{
@$rs = odbc_exec( $this->conn, $sql );
if ( $rs == false )
{
$odbc_err = odbc_errormsg( $this->conn );
$this->sys_err( "SQL语句:{{$sql}}执行失败,原因是:{{$odbc_err}}", 'die' );
}
return $rs;
}
// 获取查询返回函数
public function select_query( $sql, $retuen_res = false )
{
$res = $this->send_query( $sql );
if ( $retuen_res == true )
{
return $res;
}
switch ( strtoupper( $this->config->database ) )
{
case 'MYSQL' :
return $this->select_mysql_query( $res );
break;
case 'ACCESS' :
return $this->select_access_query( $res );
break;
default :
$this->sys_err( '数据库类型错误,该类目前只支持MYSQL与ACCESS两种数据库', 'die');
break;
}
}
// 获取MYSQL查询返回函数
private function select_mysql_query( $res )
{
$arr = array();
while ( false != ( $rs = mysql_fetch_assoc( $res ) ) )
{
$arr[] = $rs;
}
mysql_free_result( $res );
return ( count( $arr ) > 0 ? $arr : false );
}
// 获取ACCCESS查询返回函数
private function select_access_query( $res )
{
$arr = array();
while ( false != ( $rs = odbc_fetch_array( $res ) ) )
{
$arr[] = $rs;
}
odbc_free_result( $res );
return ( count( $arr ) > 0 ? $arr : false );
}
// 获取系统错误函数
public function sys_err( $err_msg, $method, $err_notice = '很抱歉,本站发生系统错误,请稍候再试。' )
{
$this->err_record( 'sys', $err_msg );
switch ( $method )
{
case 'keep':
return;
break;
default:
$this->err_notice( $err_notice );
break;
}
}
// 获取用户错误函数
public function user_err( $err_notice, $method, $re_href = '', $err_msg = '' )
{
if ( !empty( $err_msg ) )
{
$this->err_record( 'user', $err_msg );
}
switch ( $method )
{
case 'keep':
return;
break;
default:
$this->err_notice( $err_notice, $re_href );
break;
}
}
// 记录错误函数
private function err_record( $type, $err_msg )
{
$err_url = $this->the_dir . 'lib/error/';
$err_url .= ( $type == 'sys' ? 'system.err' : 'user.err' );
$record_msg = date( 'Y-m-d H:i:s' ) . '|' . $err_msg . "\n";
$this->file_put( $err_url, $record_msg, 'ab' );
}
// 文件写入函数
public function file_put( $url, $content, $method )
{
$dir = str_replace( basename( $url ), '', $url );
if ( !file_exists( $dir ) )
{
$this->sys_err( "{{$dir}}文件夹不存在", 'die' );
}
@$f = fopen( $url, $method );
@flock( $f, LOCK_NM );
@fwrite( $f, $content, strlen( $content ) );
@flock( $f, LOCK_UN );
@fclose( $f );
}
// 提示错误函数
protected function err_notice( $err_notice, $re_href = '' )
{
$err_page = $this->the_dir . $this->err_page;
if ( !file_exists( $err_page ) )
{
$this->sys_err( '错误提示页面丢失', 'keep' );
die( '很抱歉,本站发生系统错误,请稍候再试。' );
}
$err_html = file_get_contents( $err_page );
$err_html = str_replace( '<%$err_notice%>', $err_notice, $err_html);
$err_html = str_replace( '<%$this_url%>', $this->the_dir, $err_html);
$err_html = str_replace( '<%$re_href%>', $re_href, $err_html);
echo $err_html;
exit;
}
// 用于设定模版文件路径的函数
function set_dir( $file_dir = 'smarty_file' )
{
if ( !preg_match( '{/$}', $file_dir ) )
{
$file_dir .= '/';
}
if ( !file_exists( $this->the_dir . $file_dir . $this->template_dir ) )
{
$this->sys_err( 'smarty模版路径丢失', 'die' );
}
if ( !file_exists( $this->the_dir . $file_dir . $this->compile_dir ) )
{
$this->sys_err( 'smarty编译路径丢失', 'die' );
}
if ( !file_exists( $this->the_dir . $file_dir . $this->cache_dir ) )
{
$this->sys_err( 'smarty缓存路径丢失', 'die' );
}
$this->template_dir = $this->the_dir . $file_dir . $this->template_dir;
$this->compile_dir = $this->the_dir . $file_dir . $this->compile_dir;
$this->cache_dir = $this->the_dir . $file_dir . $this->cache_dir;
}
// 生成静态页面函数
public function create_html( $tpl, $file_url = '', $html_name = '' )
{
$html_tpl = $this->fetch( $tpl );
//生成静态文件的文件名
if ( empty( $html_name ) )
{
$file_name = strtolower( basename( $_SERVER['PHP_SELF'] ) );
$file_name = str_replace( '.php', ".{$this->html_cache}", $file_name );
}
else
{
$file_name = $html_name;
}
if ( !empty( $file_url ) && !preg_match( '!\/$!', $file_url ) )
{
$file_url .= '/';
}
$file_url = !empty( $file_url ) ? $this->the_dir . $file_url : "./{$file_url}";
$file_url .= $file_name;
$this->file_put( $file_url, $html_tpl, 'wb');
header("location:{$file_url}");
exit();
}
// 转到静态页面
public function goto_html( $left_time = null, $file_url = '', $html_name = '' )
{
$left_time = ( $left_time == null ? $this->html_cache_lifetime : intval( $left_time ) );
//获取静态文件的文件名
if ( empty( $html_name ) )
{
$file_name = strtolower( basename( $_SERVER['PHP_SELF'] ) );
$file_name = str_replace( '.php', ".{$this->html_cache}", $file_name );
}
else
{
$file_name = $html_name;
}
if ( !empty( $file_url ) && !preg_match( '!\/$!', $file_url ) )
{
$file_url .= '/';
}
$file_url = !empty( $file_url ) ? $this->the_dir . $file_url : "./{$file_url}";
$file_url .= $file_name;
if ( !file_exists( $file_url) )
{
return;
}
if ( $left_time == -1 )
{
header("location:{$file_url}");
exit;
}
else
{
@$fmtime = filemtime( $file_url );
$fmtime = intval( $fmtime );
if ( time() - $fmtime <= $left_time )
{
header("location:{$file_url}");
exit;
}
}
}
}
?>

复制代码 代码如下:

<?php
// 添斜线
function m_addslashes( $gpc )
{
if ( !get_magic_quotes_gpc() )
{
if( is_array( $gpc ) )
{
while( list( $k, $v ) = each( $gpc ) )
{
if( is_array( $gpc[$k] ) )
{
while( list( $k2, $v2 ) = each( $gpc[$k] ) )
{
$gpc[$k][$k2] = addslashes( $v2 );
}
@reset( $gpc[$k] );
}
else
{
$gpc[$k] = addslashes( $v );
}
}
@reset( $gpc );
}
else
{
$gpc = addslashes( $gpc );
}
}
return $gpc;
}
?>


复制代码 代码如下:

<?php
function m_check_fill( $check_arr = array() )
{
$pattern['idname'] = array('!^[a-z0-9]{3,8}$!i' , '您输入的用户名格式不正确');
$pattern['username'] = array('!^.{4,12}$!' , '您输入的用户昵称格式不正确');
$pattern['email'] = array('!^([a-z0-9]+(\.[a-z0-9]+)?@[a-z0-9]+\.[a-z0-9]+(\.[a-z0-9]+)?)?$!i' , '您输入的电子邮箱格式不正确');
$pattern['oicq'] = array('!^([0-9]{4,12})?$!' , '您输入的OICQ格式不正确');
$pattern['password'] = array('!^[a-z0-9]{6,14}$!i' , '您输入的密码格式不正确');
$pattern['real_name'] = array('!^.{4,20}$!' , '您输入的真实姓名格式不正确');
$pattern['id_card'] = array('!^[0-9]{15}([0-9]{2}[a-z0-9])?$!i' , '您输入的身份证号码格式不正确');
$pattern['title'] = array('!^.{1,255}$!' , '您输入的帖子标题格式不正确');
$pattern['block_name'] = array('!^.{3,12}$!' , '您输入的板块名称格式不正确');
$err_msg = '';
if ( !is_array( $check_arr ) )
{
return '很抱歉,系统出现参数传递错误,请通知管理员,谢谢合作';
}
foreach ( $check_arr as $key => $value )
{
if ( !empty( $pattern[$key] ) )
{
if( !preg_match( $pattern[$key][0], $value ) )
{
$err_msg .= $pattern[$key][1] . '<br>';
}
}
}
return $err_msg;
}
?>


复制代码 代码如下:

<?php
//防止跨站攻击函数
function m_check_key( $no_reload = true )
{
if ( empty( $_COOKIE['check'] ) || $_COOKIE['check'] != $_POST['check'] )
{
setcookie( 'check', '', time()-86400 );
return '发生错误,这也许是由于您重复提交数据造成的,请重新提交。<br>';
}
if ( $no_reload == true )
{
setcookie( 'check', '', time()-86400 );
}
}
?>

复制代码 代码如下:

<?php
//防止跨站攻击函数
function m_md5( $password = '' )
{
if ( empty( $password ) )
{
$password = md5( uniqid ( rand(), true ) );
setcookie( 'check', $password );
}
else
{
$key = 'machine';
$password = md5( $password . $key );
}
return $password;
}
?>

复制代码 代码如下:

<?php
//中文截取函数
function m_cnsubstr( $string, $sublen )
{
if( $sublen >= strlen( $string ) )
{
return $string;
}
$s = "";
for ( $i = 0; $i < $sublen; $i++ )
{
if( ord( $string{$i} ) > 127 )
{
$s .= $string{$i} . $string{++$i};
continue;
}
else
{
$s .= $string{$i};
continue;
}
}
return $s . '…';
}
?>

复制代码 代码如下:

<?php
// 分页类,使用时调用类下面的函数即可
class Page_class
{
//帖子总数
private $count = 0;
//每页显示多少行
private $rows;
//每页显示多少个跳转页号
private $link_num;
//共有多少页
private $pages = 0;
//当前页
private $current_page;
//开始记录
private $start = 0;
//上一大页
private $prve = 0;
//下一大页
private $next = 0;
//数字链接
private $links = array();
//返回值
public $return_rs = array();
public function __construct( $count, $current_page = 1, $rows = 10, $link_num = 7 )
{
//获取传入的帖子总数
$this->count = intval( $count );
//获取当前页
$this->current_page = intval( $current_page );
//显示多少行
$this->rows = intval( $rows );
//每页显示多少个跳转页号
$this->link_num = $link_num;
//调用计算页数的方法
$this->count_page();
//调用返回跳转页号的方法
$this->return_links();
//返回值
$this->return_rs = array(
'rows' => $this->rows,
'prve' => $this->prve,
'next' => $this->next,
'pages' => $this->pages,
'start' => $this->start,
'count' => $this->count,
'links' => $this->links,
'current_page' => $this->current_page
);
}
public function __destruct()
{
}
//计算页数
private function count_page()
{
//计算共有多少页
@$this->pages = ceil( $this->count / $this->rows );
//如果当前页大于最大页数,则使其等于最大页数;如果当前页小于1,则使其等于1
$this->current_page > $this->pages ? $this->current_page = $this->pages : true ;
$this->current_page < 1 ? $this->current_page = 1 : true;
//计算查询的开始记录数
$this->start = ( $this->current_page - 1 ) * $this->rows;
}
//返回页面跳转页号的函数
private function return_links()
{
//用当前页除以显示页数得到当前是第几“大页”
$start_s = floor( $this->current_page / $this->link_num );
//如果当前页号正好整除显示页数,则应该对$start_s减一,因为设想一下,如果当前是第7页
//显示页数也是7,则$start_s=1,也就是说已经到了第二“大页”了,而实际上它应该还是在
//第一“大页”
( $this->current_page % $this->link_num ) == 0 ? $start_s-- : true;
//计算当前“大页”开始页号,算法是(当前“大页”*显示页数)+1;例如0*7+1=1,1*7+1=8,2*7+1=15
$start_page = ( $start_s * $this->link_num ) + 1;
//上一大页
$this->prve = $start_page - 1;
//下一大页
$this->next = $start_page + $this->link_num;
//开始循环计算当前大页中的小页号
for ( $i=0; $i < $this->link_num; $i++ )
{
//如果下一个页号已经超出了总页数,则说明应该停止了
if ( $start_page + $i > $this->pages )
{
break;
}
//将页号记录在$this->links_arr数组中
$this->links[] = $start_page + $i;
}
}
}
function m_page( $count, $current_page = 1, $rows = 10, $link_num = 7 )
{
$page = new Page_class( $count, $current_page, $rows, $link_num );
return $page->return_rs;
}
?>


复制代码 代码如下:

<?php
// 文本操作函数
// 修改
function m_txt_replace( $pattern, $text, $content )
{
$pattern_start = "<!--$pattern-->";
$pattern_end = "<!--/$pattern-->";
@$ok = preg_match( "{{$pattern_start}.*{$pattern_end}}Ssi", $content, $match );
if ( $ok != true )
{
return false;
}
$replace = "{$pattern_start}{$text}{$pattern_end}";
$new_content = str_replace( $match[0], $replace, $content );
return $new_content;
}
// 追加
function m_txt_add( $pattern, $text, $content )
{
$pattern = "<!--{$pattern}-->";
@$ok = preg_match( "{{$pattern}}Ssi", $content );
if ( $ok != true )
{
return false;
}
$add = "{$pattern}\n{$text}";
$new_content = str_replace( $pattern, $add, $content );
return $new_content;
}
// 删除
function m_txt_delete( $pattern, $content )
{
$pattern_start = "<!--$pattern-->";
$pattern_end = "<!--/$pattern-->";
@$ok = preg_match( "{{$pattern_start}.*{$pattern_end}}Ssi", $content, $match );
if ( $ok != true )
{
return false;
}
$new_content = str_replace( $match[0], '', $content );
return $new_content;
}
//获取
function m_txt_get( $pattern, $content )
{
$pattern_start = "<!--$pattern-->";
$pattern_end = "<!--/$pattern-->";
@$ok = preg_match( "{{$pattern_start}.*{$pattern_end}}Ssi", $content, $match );
if ( $ok != true )
{
return false;
}
return $match[0];
}
?>


复制代码 代码如下:

<?php
// 上传函数
function m_up_file( $files, $up_url, $type, $max_size = 2097152 )
{
$i = 0;
if ( !is_array( $files ) )
{
die( '参数传递错误' );
}
$type_pattern = is_array( $type ) ? '\.(' . implode( ')|(', $type ) . ')' : "\.({$type})";
foreach ( $files as $key => $arr )
{
$ok = false;
if( $arr['error'] == 0 )
{
if ( !is_uploaded_file( $arr['tmp_name'] ) )
{
$err_msg .= "文件:<b>{$arr['name']}</b>不可上传<br>";
continue;
}
elseif ( $_FILES['up_file']['size'] > $max_size )
{
$err_msg .= "文件:<b>{$arr['name']}</b>上传失败,原因是:文件超过限定大小<br>";
continue;
}
elseif ( !preg_match( "!{$type_pattern}!Si", $arr['name'] ) )
{
$err_msg .= "文件<b>{$arr['name']}</b>上传失败,原因是:格式不正确<br>";
continue;
}
else
{
$txt = substr( str_shuffle( 'abcdefghijklmnopqrstuvwxyz' ), -4 );
$hz = strtolower( strstr( $arr['name'], '.' ) );
$new_name = date( 'YmdHis' ) . $txt . $hz;
if ( !is_array( $up_url ) )
{
!preg_match( '!\/$!', $up_url ) ? $up_url .= '/' : true;
$new_url = $up_url . $new_name;
}
else
{
$key = str_replace( '.', '', $hz );
$up_url = array_change_key_case( $up_url, CASE_LOWER );
!preg_match( '!\/$!', $up_url[$key] ) ? $up_url[$key] .= '/' : true;
$new_url = $up_url[$key] . $new_name;
}
@$ok = move_uploaded_file( $arr['tmp_name'], THIS_DIR . $new_url );
}
}
if ( $ok == true )
{
$rs[$i]['url'] = $new_url;
$rs[$i]['name'] = $arr['name'];
$rs[$i]['type'] = strtoupper( str_replace( '.', '', $hz ) );
$i++;
}
elseif( !empty($arr['name']) )
{
$err_msg .= "文件<b>{$arr['name']}</b>上传出错<br>";
continue;
}
}
return array( 'arr' => $rs, 'err_msg' => $err_msg, 'num' => $i );
}
?>