duanlinpi0265 2013-01-11 12:26
浏览 163
已采纳

包含在包含的文件中不适用于相对路径

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

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

i get the following output :

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

In a nutshell:

  1. The first line tells that the file is not a part of my imagination and it actually exists.
  2. The second line tells the location of my calling script, on examining the paths closely, you'll notice that the file I'm trying to include resides in the parent directory
  3. Include function says that file dosent exist

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

Update

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

Update 2

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??

展开全部

  • 写回答

3条回答 默认 最新

  • douwen3127 2013-01-11 12:38
    关注

    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. 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.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 syri可视化不显示插入缺失
  • ¥30 运行软件卡死查看系统日志分析不出来
  • ¥15 C语言代码改正特征选择算法设计,贝叶斯决策,,设计分类器,远程操作代码修正一下
  • ¥15 String 类valuve指向的问题
  • ¥15 在ros2的iron版本进行编译时遇到如下问题
  • ¥18 vs用setup project打包项目实现安装完立即运行
  • ¥15 孟德尔随机化TwoSampleMR在线提取结局数据,遇到Error in check_reset(override_429)的问题
  • ¥15 ONNX转RKNN遇到问题
  • ¥60 以太网电缆未接通怎么处理
  • ¥15 关于超声图片进行放射组学的疑问
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部