包含在包含的文件中不适用于相对路径 [英] Include within an included file is not working with relative paths

查看:107
本文介绍了包含在包含的文件中不适用于相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个错误在过去64分钟内耗尽了我的理性思考能力,我真的不知道该怎么办.

This bug has depleted my rational thinking power over the past 64 minutes and I really don't know what I should do.

当我在包含的文件中使用以下3行时:

When i use the following 3 lines in an included file:

var_dump(file_exists('G:/xampp/htdocs/myproject/public_html/includes/htmlPurifierAndMarkdown.php')); //retuurns true
var_dump(__FILE__);
include '../htmlPurifierAndMarkdown.php'; //gives error

我得到以下输出:

boolean true

string 'G:\xampp\htdocs\myproject\public_html\includes\classes\genral.class.php' (length=71)


( ! ) Warning: include(../htmlPurifierAndMarkdown.php) [function.include]: failed to open stream: No such file or directory in G:\xampp\htdocs\myproject\public_html\includes\classes\genral.class.php on line 4
Call Stack
#   Time    Memory  Function    Location
1   0.0005  345328  {main}( )   ..\add.php:0
2   0.0036  382408  include( 'G:\xampp\htdocs\myproject\public_html\includes\classes\events.class.php' )    ..\add.php:28
3   0.0041  398024  include( 'G:\xampp\htdocs\myproject\public_html\includes\classes\standardpost.class.php' )  ..\events.class.php:2
4   0.0047  413928  include( 'G:\xampp\htdocs\myproject\public_html\includes\classes\genral.class.php' )    ..\standardpost.class.php:2

简而言之:

  1. 第一行表明该文件不是我的想象力的一部分,它实际上是存在的.
  2. 第二行告诉我调用脚本的位置,在仔细检查路径时,您会注意到我要包含的文件位于父目录中
  3. include函数表示文件文件存在

所以我只是想像一下东西,还是这是一个严重的PHP错误?

so am i just imagining stuff or is this some serious php bug?

当我手动访问genral.class.php时,不会产生任何错误.从包含的文件中包含时,有什么特殊的规则适用吗?

when i access genral.class.php manually, no error is generated. do special rules apply when including from within an included file?

更新2

当我将include路径设置为'../includes/htmlPurifierAndMarkdown.php'时,这是相对于主调用脚本的路径,而该主调用脚本又包含一个包含genral.class.php的脚本,它可以工作,但是由于新include仅与许多调用脚本之一有关,而对于其他调用脚本却不起作用.从包含的文件中包含文件时,是否适用特殊规则?

When i set the include path to '../includes/htmlPurifierAndMarkdown.php' , which is the path relative to the main calling script, which in turn includes a script which includes genral.class.php it works, but since this new include is relative to only one of the many calling scripts, it breaks down for the others. Do special rules apply when including a file from within an included file??

推荐答案

__FILE__常量返回包含该行的文件的完整路径和文件名. 该文件可能位于当前工作目录以外的其他目录中.在您的特定示例中,您可以使用以下代码:

The __FILE__ constant returns the full path and filename of the file containing that line. It is possible for this file to be located inside a directory other than the current working directory. In your particular example you can use this:

include dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'htmlPurifierAndMarkdown.php';


可能发生的情况的示例如下:


An example of what might be going on is as follows:

  1. 正在执行的文件是/example.com/public_html/index.php
  2. 当前工作目录为/example.com/public_html/
  3. 索引文件包含/example.com/public_html/includes/classes/genral.class.php
  4. 以上文件尝试包含../htmlPurifierAndMarkdown.php
  5. ..是相对于当前工作目录而不是相对于所包含文件的转换.因此,您最终得到/example.com/htmlPurifierAndMarkdown.php和讨厌的错误消息.
  1. The file being executed is /example.com/public_html/index.php
  2. Current working directory is /example.com/public_html/
  3. The index file includes /example.com/public_html/includes/classes/genral.class.php
  4. The above file attempts to include ../htmlPurifierAndMarkdown.php
  5. The .. is translated relative to current working directory and not relative to the included file. Thus you end up with /example.com/htmlPurifierAndMarkdown.php and a nasty error message.

这篇关于包含在包含的文件中不适用于相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆