【问题标题】:Restructuing the code structure is giving me an include issue in php重构代码结构给了我一个 php 中的包含问题
【发布时间】:2011-12-08 10:48:31
【问题描述】:

我重组了我已经工作的代码。我之前的目录结构如下:

  • 网络
    • CSS
      • style.css
    • 型号
      • db_functions.inc
    • 查看
      • view_functions.inc
    • 控制器
    • index.php

这里是运行的代码 sn-p:

<?php
    $pageTitle = "Citee.me";

    //include view functions
    include ('view/view_functions.inc');
    //incldue db functions
    include ('model/db_functions.inc');
    doHtmlHeader($pageTitle);
    doBody();
    testMySQL();
    doHtmlFooter();
?>

我现在将代码重组为:

  • 网络
    • 型号
      • db_functions.inc
    • 查看
      • view_functions.inc
    • 控制器
  • 公开
    • CSS
      • style.css
    • js
    • 图片
    • index.php

现在我修改了我的 index.php 代码如下(我注释掉了 db 函数以缩小问题范围):

 <?php
    $pageTitle = "Citee.me";

    //include view functions
    include ('/citee/web/view/view_functions.inc');
    //incldue db functions
    //include ('./model/db_functions.inc');
    doHtmlHeader($pageTitle);
    //doBody();
    //testMySQL();
    doHtmlFooter();
?>

我得到错误:

PHP Warning:  include(/citee/web/view/view_functions.inc): failed to open stream: No such file or directory in /Library/WebServer/Documents/citee/public/index.php on line 6

PHP Warning:  include(): Failed opening '/citee/web/view/view_functions.inc' for inclusion (include_path='.:/usr/lib/php') in /Library/WebServer/Documents/citee/public/index.php on line 6

PHP Fatal error:  Call to undefined function doHtmlHeader() in /Library/WebServer/Documents/citee/public/index.php on line 9

不知道为什么我会收到此错误。我的包含路径错了吗?我尝试了不同的组合,但没有帮助。

提前致谢。

【问题讨论】:

    标签: php path include


    【解决方案1】:

    PHP 是您的朋友,它为您提供了执行代码的完整路径:

    /Library/WebServer/Documents/citee/

    不是

    /citee/

    仅仅因为您无法访问 Web 根目录之外的文件并不意味着 PHP 不会在您的 Web 根目录之外查找。

    【讨论】:

    • +1 - 总而言之,animuson在这里说的是你需要澄清你的路径是相对的还是绝对的。如果您使用的是相对路径,请从路径中删除前导 /
    • 我使用了真实路径 ../web/view/view_functions.inc 并且它有效。感谢您的回复。
    【解决方案2】:

    在第二个示例中,您的路径是绝对路径并以“/citee”开头。但错误消息显示您的文件位于“/Library/WebServer/Documents/citee”。如果您使用的是绝对路径,则必须使用正确的路径。

    【讨论】:

      【解决方案3】:

      PHP 抱怨路径/citee/web/view/view_functions.inc 不存在,我也对此表示怀疑。你能cd /citee/web/view/吗?我打赌不会。

      如果您尝试访问它,您可以使用相对文件路径来访问它(这是一种更好的做法,考虑将来部署到不同的结构化服务器文件系统)。

      因此,您可以从 index.php 的路径向正确的相对路径工作,而不是 /citee/web/view/view_functions.inc../web/view/view_functions.inc.. 代表“在树中上一个目录”,意思是它会去进入您的“citee”文件夹,然后访问“web”文件夹,依此类推。)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-17
        • 2011-07-21
        • 1970-01-01
        • 2022-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多