php 包含包含另一个 php 文件
我现在脑子很乱,脑子很痛! :( 哈哈
根:
- index.php
包含:
- cat.php
- dog.php
索引包含狗:include("includes/dog.php");
狗包含 cat: include("cat.php");
当我运行索引时,对于cat 它说:
- 无法建立到服务器的链接
- 用户拒绝访问...
但是,如果我运行狗,我不会遇到任何问题...
我猜它是路径,但我已经尝试过 ./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:
- A link to the server could not be established
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是因为当您包含相对路径时,它是相对于入口点(第一个 PHP 文件,由 Web 服务器调用)的。
在狗身上,做
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
这取决于您正在执行的脚本所在的位置。当您执行
/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 includingcats.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 findcats.php
.Read Bart's comment on how to solve this.
解决这个问题的另一种方法是设置文件的包含路径,看看这个。
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
感谢这个好帖子。
我用巴特的答案来解决这个问题。但我这里还有一个问题。
让我惊讶的是,即使不使用 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 atinclude_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.