php 包含包含另一个 php 文件

发布于 2024-09-05 22:24:07 字数 368 浏览 2 评论 0原文

我现在脑子很乱,脑子很痛! :( 哈哈

根:

  • index.php

包含:

  • cat.php
  • dog.php

索引包含狗:include("includes/dog.php");

狗包含 cat: include("cat.php");

当我运行索引时,对于cat 它说:

  1. 无法建立到服务器的链接
  2. 用户拒绝访问...

但是,如果我运行狗,我不会遇到任何问题...

我猜它是路径,但我已经尝试过 ./includes /cat.php 不高兴...

I'm getting really muddled up now and my brain hurts! :( lol

Root:

  • index.php

Includes:

  • cat.php
  • dog.php

index includes dog: include("includes/dog.php");

dog includes cat: include("cat.php");

When I run index, for cat it says:

  1. A link to the server could not be established
  2. Access denied for user ...

However, if I run dog, I get no problems...

I'm guessing its the path, but i've tried ./includes/cat.php to no joy...

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

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

发布评论

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

评论(4

谎言 2024-09-12 22:24:07

这是因为当您包含相对路径时,它是相对于入口点(第一个 PHP 文件,由 Web 服务器调用)的。

在狗身上,做

include(dirname(__FILE__) . '/cat.php'); // __FILE__ is always the name of the php file it's in

This is because when you include a relative path, it's relative to the entry point (the first PHP file, called by the webserver).

In dog, do

include(dirname(__FILE__) . '/cat.php'); // __FILE__ is always the name of the php file it's in
自由如风 2024-09-12 22:24:07

这取决于您正在执行的脚本所在的位置。当您执行 /index.php 时,脚本的路径设置为 /,因此所有包含都从那里开始。这意味着您可以找到/includes/dog.php,但不可能找到/cats.php。请注意,即使您从 /includes/dog.php 脚本中包含 cats.php,这也不会更改原始执行路径。

另一方面,当您执行 /includes/dog.php 时,您的路径设置为 /includes/,这就是为什么 PHP 也可以找到 cats.php

阅读巴特关于如何解决这个问题的评论。

It depends on where the script you are executing lies. When you execute /index.php the path of the script set to /, so all includes start from there. This means that you can find /includes/dog.php, but it's not possible to find /cats.php. Mind that, even if you are including cats.php from your /includes/dog.php script, this doesn't change the original execuption path.

When, on the other hand, you are executing /includes/dog.php, your path is set to /includes/, which is why PHP can also find cats.php.

Read Bart's comment on how to solve this.

霊感 2024-09-12 22:24:07

解决这个问题的另一种方法是设置文件的包含路径,看看这个。

http://ve2.php.net/manual/en/function .set-include-path.php

Another way to solve this is to set the include path of files, take a look at this.

http://ve2.php.net/manual/en/function.set-include-path.php

吾家有女初长成 2024-09-12 22:24:07

感谢这个好帖子。

我用巴特的答案来解决这个问题。但我这里还有一个问题。

让我惊讶的是,即使不使用 dirname(__FILE__) ,它也能在我朋友的系统中工作,所以我几乎没有做任何研究并比较了两个 php.ini 文件。我注意到 php.ini 中的 include_path 参数几乎没有区别。

在我的 php.ini 中,它设置为 Pear 目录。所以我注释掉只是为了测试,令我惊讶的是它有效。这时我意识到我们需要包含一些我不知道的文件夹或将其注释掉,以便它采用默认值。

Thanks for this nice thread.

I used bart's answer to solve this issue. But I still have one question here.

I was surprised that it worked in my mate's system even without using dirname(__FILE__) so I did little research and compared both php.ini files. I noticed there is little difference at include_path parameter in php.ini.

In my php.ini it is set to Pear directory. So I commented out just to test and to my wonder it worked. This is when I realized we need to include some folder which I dont know or comment it out so that it takes default value.

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