Discuz!开发之文件加载路径函数libfile()介绍

本文介绍了一个用于PHP项目的自定义库文件加载函数libfile()。该函数通过指定库名和可选的文件夹参数来查找并返回库文件的真实路径。文章详细解释了函数的工作原理,并提供了具体的使用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

定义位置:
source\function\function_core.php
函数代码

function libfile($libname, $folder = '') {
	$libpath = '/source/'.$folder;
	if(strstr($libname, '/')) {
		list($pre, $name) = explode('/', $libname);
		$path = "{$libpath}/{$pre}/{$pre}_{$name}";
	} else {
		$path = "{$libpath}/{$libname}";
	}
	return preg_match('/^[\w\d\/_]+$/i', $path) ? realpath(DISCUZ_ROOT.$path.'.php') : false;
}

函数介绍

libfile($libname, $folder = '')

包含2个参数:$libname 和 $folder

该函数读取source目录下的$folder子目录作为基础部分。

例如 require_once libfile('function','plugin/test/');

实际为 require_once /source/plugin/test/function.php

另外当$libname中包含/的时候,把$libname分为前后两部分,前面部分为子目录。

规则为:/source/$folder/$libname_pre/$libname_pre_$libname_end.php

实例一:

require_once libfile('function/cache');

实际为 require_once /source/function/function_cache.php

实例二:

require_once libfile('portal/view', 'module');

实际为:require_once /source/module/portal/portal_view.php

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值