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

为什么是PHP;要求;找不到文件?

为什么是PHP;要求;找不到文件?,php,include,require,include-path,Php,Include,Require,Include Path,我有以下文件结构: temp main index.php a.php b.php 这是档案 index.php echo "index.php ---> " . __DIR__ . "<br />"; require('../a.php'); echo "OK<br />" 我注意到如果我改变 require('./b.php'); 到 为什么呢? 它按预期工作。使用require('..\a.php') sayi

我有以下文件结构:

temp
    main
        index.php
    a.php
    b.php
这是档案

index.php

echo "index.php ---> " . __DIR__ . "<br />";

require('../a.php');

echo "OK<br />"
我注意到如果我改变

require('./b.php');

为什么呢? 它按预期工作。

使用
require('..\a.php')

say
include
对Windows使用反斜杠

编辑:


哎呀。我误读了密码。忽略此答案。

当您使用include或require时,所包含的文件将充当包含它的脚本的一部分。在这种情况下,文件
a.php
可能与
b.php
位于同一目录中,但当代码运行时,它在
index.php
的上下文中运行。如果您使用的是
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
,而不是
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
,那么当
a.php作为包含文件在
index.php
上运行时,您会看到
返回与
index


由于相对路径根据文件的使用位置而变化,因此最好始终使用相对于服务器根的绝对路径。如果机器配置正常,将从
$\u SERVER[“DOCUMENT\u ROOT”]
开始,然后添加应用程序路径和includes路径。在某些共享托管服务器上,您可能需要在某个位置硬编码根目录(或将其添加到.htaccess文件)。

包括
a.php
似乎可以工作,但没有错误-1:如手册中所述,斜杠也支持跨平台兼容性。不支持。为了便于携带,您应该使用
目录分隔符
常量,尽管正斜杠或反斜杠都可以在Windows上使用。@Phil-PHP自己的开发人员不鼓励使用目录分隔符。@Phil Brown:PHP的核心开发人员不鼓励使用
目录分隔符
,并建议使用正斜杠
//code>。此外,是的,Windows支持正斜杠,请参阅。
echo "b is here<br />"
index.php ---> D:\Programs\WampServer 2\www\temp\main
a.php ---> D:\Programs\WampServer 2\www\temp

Warning: require(./b.php) [function.require]: failed to open stream: No such file or directory in D:\Programs\WampServer 2\www\temp\a.php on line 5

Fatal error: require() [function.require]: Failed opening required './b.php' (include_path='.;C:\php5\pear') in D:\Programs\WampServer 2\www\temp\a.php on line 5
require('./b.php');
require('b.php');