为什么 PHP“需要”没有找到文件?

发布于 2024-09-28 16:27:31 字数 1095 浏览 6 评论 0原文

我有以下文件结构:

temp
    main
        index.php
    a.php
    b.php

这是文件;

index.php

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

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

echo "OK<br />"

a.php

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

require('./b.php');

echo "a is here<br />"

b.php

echo "b is here<br />"

当调用 index.php 时,我收到以下错误:

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');

为什么是这样? 它按预期工作。

I have the following files structure:

temp
    main
        index.php
    a.php
    b.php

Here are the files;

index.php

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

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

echo "OK<br />"

a.php

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

require('./b.php');

echo "a is here<br />"

b.php

echo "b is here<br />"

When index.php is called I got the following error:

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

I have noticed that if I change

require('./b.php');

to

require('b.php');

Why is that ?
it works as expected.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感悟人生的甜 2024-10-05 16:27:31

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

由于相对路径会根据文件的使用位置而变化,因此最好始终使用相对于服务器根目录的绝对路径。如果机器配置正常,将以 $_SERVER["DOCUMENT_ROOT"] 开头,然后添加应用程序路径和包含路径。在某些共享托管服务器上,您可能必须在某处对根进行硬编码(或将其添加到 .htaccess 文件中)。

When you use include or require, the file that you include will act as if it is part of the script that included it. In this case, the file a.php might live in the same directory as b.php, but when the code is running, it is running in the context of index.php. If you had used __FILE__ rather than __DIR__, you would see that a.php returns the same value as index.php when it is running as an included file on index.php.

Since the relative path changes depending on where files are used, it's always best to use an absolute path relative to the server root. If the machine is configured normally, that will start with $_SERVER["DOCUMENT_ROOT"], then add the application path and the includes path. On some shared hosting servers, you might have to hard-code the root somewhere (or add it to the .htaccess file).

捂风挽笑 2024-10-05 16:27:31

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

PHP 手册include 在 Windows 中使用反斜杠。

编辑:

哎呀。我看错代码了。忽略这个答案。

Use require('..\a.php');

The PHP manual says include uses backslashes for Windows.

Edit:

Oops. I misread the code. Disregard this answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文