douzhang2680
2015-07-20 10:14
浏览 56
已采纳

包含一个公共文件只在一个文件Codeigniter中失败

i am including "navigation.php" which has the common menu in all my views .

<div id="navigation">
    <? include("navigation.php"); ?>
</div>

this code works in every other files but in one file it gives errors .

Severity: Warning
Message: include(navigation.php): failed to open stream: No such file or directory
Filename: views/readsinvo_index.php
Line Number: 2


Severity: Warning
Message: include(): Failed opening 'navigation.php' for inclusion (include_path='.:/usr/share/pear:/usr/share/php')
Filename: views/readsinvo_index.php
Line Number: 2

in "readsinvo_index.php" it is same as the other files

<div id="navigation">
    <? include("navigation.php"); ?>
</div>

and both the files are inside "views" . both files have permission 777 .

But when i tried with

<? include(dirname(__FILE__)."/navigation.php"); ?>

It works perfectly , but in all other pages , it is working when used

<? include("navigation.php"); ?>

is there any possible reason that i cannot include the file normally only in this file . I am using codeigniter framework and CentOS .

图片转代码服务由CSDN问答提供 功能建议

我包含“navigation.php”,它在我的所有视图中都有公共菜单。

 &lt; div id =“navigation”&gt; 
&lt;? 包括( “navigation.php”);  ?&gt; 
&lt; / div&gt; 
   
 
 

此代码适用于所有其他文件,但在一个文件中,它会产生错误。 < pre> 严重性:警告 消息:include(navigation.php):无法打开流:没有这样的文件或目录 文件名:views / readsinvo_index.php Line编号:2 Severity:Warning \ nMessage:include():无法打开'navigation.php'以包含(include_path ='。:/ usr / share / pear:/ usr / share / php') 文件名:views / readsinvo_index.php Line编号:2 \ n

在“readsinvo_index.php”中它与其他文件相同

 &lt; div id =“navigation  “&gt; 
&lt;? 包括( “navigation.php”);  ?&gt; 
&lt; / div&gt; 
   
 
 

并且这两个文件都位于“视图”中。 两个文件都有权限777。

但当我尝试

 &lt;? 包括(目录名(FILE __ __) “/ navigation.php”。);  ?&gt; 
   
 
 

它完美无缺,但在所有其他页面中,它在使用时正常工作

 &lt  ;? 包括( “navigation.php”);  ?&gt; 
   
 
 

是否有任何可能的原因导致我无法在此文件中正常包含该文件。 我正在使用codeigniter框架和CentOS。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongzai2952 2015-07-20 11:19
    已采纳

    You can use APPPATH

    APPPATH.'views/navigation.php'
    

    And Possible duplicate of how call another php file to Codeigniter view

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongyin8009 2015-07-20 10:19

    CodeIgniter runs from a single entry script file index.php.

    So, when you say current directory, it is the path of index.php's directory.

    If you include some file, it will first search for any file existing in the given directory, then in include path.

    The statement <? include(dirname(__FILE__)."/navigation.php"); ?>

    The dirname gives you path of index.php.

    When you add statement: <? include("/navigation.php"); ?> in views, it will search for the views directory, hence, the error.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题