Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.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_Include_Require Once - Fatal编程技术网

PHP需要“/”的问题

PHP需要“/”的问题,php,include,require-once,Php,Include,Require Once,我在根目录中有一个index.php文件: <?php require_once "./include/common.php"; ?> 和include文件夹中的common.php文件: <?php require_once "globalConfig.php"; ?> 文件globalConfig与common.php位于同一文件夹中。将树文件夹设置为: xxx/index.php xxx/include/common.php xxx/include/global

我在根目录中有一个index.php文件:

<?php
require_once "./include/common.php";
?>
和include文件夹中的common.php文件:

<?php
require_once "globalConfig.php";
?>
文件globalConfig与common.php位于同一文件夹中。将树文件夹设置为:

xxx/index.php

xxx/include/common.php

xxx/include/globalConfig.php

php正常运行。但如果我将文件common.php更改为:

<?php
require_once "./globalConfig.php";
?>

PHP显示一条警告,表示找不到globalConfig.PHP文件。有什么区别?我认为在./的情况下,最外部的文件index.php将在其当前目录中找到globalConfig.php。

我认为路径,即../是相对于基本脚本的,而不是它所包含的文件。因此,在包含脚本时,我通常使用绝对路径。

我认为路径,即../是相对于基本脚本的,而不是它所包含的文件。因此,在包含脚本时,我通常使用绝对路径。

该路径相对于当前工作目录;在web上下文php cgi中,这是最初调用的脚本所在的目录,该脚本直接启动,而不是通过include。 一个简单的解决方法:

require_once dirname(__FILE__) . '/foobar.inc.php';

路径相对于当前工作目录;在web上下文php cgi中,这是最初调用的脚本所在的目录,该脚本直接启动,而不是通过include。 一个简单的解决方法:

require_once dirname(__FILE__) . '/foobar.inc.php';
在index.php中添加

define('BASE_PATH',str_replace('\\','/',dirname(__FILE__)));
然后在common.php中包含这样的内容

require_once BASE_PATH . '/includes/globalConfig.php';
这将找到确切的路径并标准化斜杠,然后无论何时使用BASE_path,都会从htdocs的根目录(即index.php)中包含斜杠。

define('BASE_PATH',str_replace('\\','/',dirname(__FILE__)));
然后在common.php中包含这样的内容

require_once BASE_PATH . '/includes/globalConfig.php';

这将找到确切的路径并标准化斜杠,然后无论何时使用BASE_path,都会从htdocs的根目录(即index.php)中包含。另请参见,搜索php时的许多其他问题也包括path:另请参见,搜索php还有很多其他问题,包括path:当str_replace将反斜杠转换为斜杠时,我在每个项目中都使用它。不客气。当str_replace将反斜杠转换为斜杠时,我会在每个项目中使用它。欢迎光临。