你用的是相对路径,当然打开文件的时候以当前工作目录为主。一般作法是传参数,而不是固定在类里,smarty模板库也是这样的:$smarty->display('template/message.htm');
你就可以这样调用 $message->showmessage($contents, './template/message.htm');
换成子目录就这么调用$message->showmessage($contents, '../template/message.htm');或者干脆用绝对路径:
$config['AbsolutePath'] = "C:/template";// 这是模板目录配置
function showmessage($contents)
{
global $config;
$temp=file_get_contents("{$config['AbsolutePath']}/message.htm");
echo str_replace('{message}',$contents,$temp);
}
你就可以这样调用 $message->showmessage($contents, './template/message.htm');
换成子目录就这么调用$message->showmessage($contents, '../template/message.htm');或者干脆用绝对路径:
$config['AbsolutePath'] = "C:/template";// 这是模板目录配置
function showmessage($contents)
{
global $config;
$temp=file_get_contents("{$config['AbsolutePath']}/message.htm");
echo str_replace('{message}',$contents,$temp);
}
一般用绝对路径就可以了,我一般就这样弄
楼上的用配置一个常量也不错
在虚拟主机上不知道路径阿
咋办
|-index.php
|-config.php
|-lib
|-class
|-db.php
|-images做一个配置文件config.php放到网站根目录
在配置文件中设置系统根目录
@define( "APP_ROOT", dirname( __FILE__ ) );其它文件包含该配置文件,在任何地方使用APP_ROOT都是以网站根目录为基础
如在index.php中想使用db.php
那么 require_once( APP_ROOT . "/lib/class/db.php" );