Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中需要所有目录中的初始化文件_Php_Directory_Init - Fatal编程技术网

在PHP中需要所有目录中的初始化文件

在PHP中需要所有目录中的初始化文件,php,directory,init,Php,Directory,Init,所以我在一个名为“core”的目录中有一个名为“init.php”的初始化文件。在这个init文件中,我在一个名为“classes”的目录中“required”了所有其他必需的组件文件,因此init.php有几行“require_once”。每次我在根目录中创建一个文件(例如创建“index.php”),我都必须包含这个文件,即“include core/init.php”。一切都很顺利,直到我尝试将这个init文件包含在另一个目录中创建的文件中。例如,在名为“levels/index.php”

所以我在一个名为“core”的目录中有一个名为“init.php”的初始化文件。在这个init文件中,我在一个名为“classes”的目录中“required”了所有其他必需的组件文件,因此init.php有几行“require_once”。每次我在根目录中创建一个文件(例如创建“index.php”),我都必须包含这个文件,即“include core/init.php”。一切都很顺利,直到我尝试将这个init文件包含在另一个目录中创建的文件中。例如,在名为“levels/index.php”的目录中。每次在根目录以外的目录中包含此init文件时,我都会收到一个“include”错误

下面是我的init.php文件的外观:

//load all class files automatically
spl_autoload_register(function($class) {
    require_once 'classes/' . $class . '.php';
});

//include functions
require_once 'functions/sanitize.php';
require_once 'functions/functions.php;
include '../../core/init.php';
现在,位于根目录的index.php文件如下所示:

include 'core/init.php';
没有显示任何错误,但是名为“levels”的目录中的另一个PHP文件(例如source.PHP)触发了一个错误

警告:require_once(functions/sanitize.php):无法打开流

下面是我的source.php文件的外观:

//load all class files automatically
spl_autoload_register(function($class) {
    require_once 'classes/' . $class . '.php';
});

//include functions
require_once 'functions/sanitize.php';
require_once 'functions/functions.php;
include '../../core/init.php';

我希望有一个解决方案,因为我将有麻烦维护我所有的文件只位于根目录。有时我会想在其他目录中的文件中包含初始化文件。提前谢谢。

您应该使用绝对路径。首先定义一个路径,仅exmaple:
define('ROOT\u path','localshot/')
然后包括类似于
require\u once ROOT\u path的文件core/init.php'

您应该使用绝对路径。首先定义一个路径,仅exmaple:
define('ROOT\u path','localshot/')
然后包括类似于
require\u once ROOT\u path的文件core/init.php'