require_once 不能加载被包孕的文件里的内容
Jun 13, 2016 am 10:53 AM
require_once 不能加载被包含的文件里的内容
网站目录结构:
rootdir(D:\test)
.....index.php
.....class
.....config.php
.....db.class.php
内容:
index.php
require_once('class/db.class.php');
?>
config.php
$db_config['hostname'] = '192.168.1.22';
?>
问题:为什么写法1能正常运行,写法2不行呢
写法1:
db.class.php
require_once('D:/test/class/config.php');
echo $db_config['hostname'] ;
?>
访问:http://localhost/test/index.php
能正常输出:192.168.1.12
写法2:
db.class.php
require_once('config.php');
echo $db_config['hostname'] ;
?>
访问:http://localhost/test/index.php
不能正常输出:
------解决方案--------------------
require_once dirname(__FILE__) . '/config.php';
路径问题,用上面这个就可以了
------解决方案--------------------
首先,你的写法2并没有错
你用
print_r(get_included_files());
看看都加载了那些文件
估计与 test 所在目录中也有个 config.php

熱門文章

熱門文章

熱門文章標籤

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南

如何設定 Visual Studio Code (VS Code) 進行 PHP 開發
