【问题标题】:php how to do include right wayphp如何做正确的方法
【发布时间】:2015-02-25 15:29:12
【问题描述】:

您好,我正在运行 WAMP(32 位)服务器

我使用带有此地址http://localhost:8080/1/的浏览器执行C:\wamp\www\1\index.php

index.php 有如下代码

require_once 'libraries/database/database.php';

database.php 有如下代码

require_once '../misc/traits/singleton.php';

singleton.php 位于此位置

C:\wamp\www\1\libraries\misc\traits\singleton.php

database.php 位于此位置

C:\wamp\www\1\libraries\database\database.php

并且有错误:

( ! ) 警告:require_once(../misc/traits/singleton.php):打开流失败:C:\wamp\www\1\libraries\database\database.php 中没有这样的文件或目录第 3 行

( ! ) 致命错误:require_once(): 无法在 C:\wamp\www\ 中打开所需的 '../misc/traits/singleton.php' (include_path='.;C:\php\pear') 1\libraries\database\database.php 在第 3 行

谢谢

【问题讨论】:

    标签: php path include


    【解决方案1】:

    路径源自最初执行的 php 文件,除非您使用 chdir 更改路径。

    所以使用'libraries/misc/traits/singleton.php'

    如果要进行相对包含,请使用__DIR__,即当前文件所在的目录。

    例如

    __DIR__ . '/../misc/traits/singleton.php'

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-20
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    相关资源
    最近更新 更多