【问题标题】:php include path on windowsphp在windows上包含路径
【发布时间】:2016-04-01 00:32:41
【问题描述】:

我在 Windows 上使用 PHP 工作了几天。我被指示的整个时间包括以下方式:

在 index.php 文件中:

require ('include/init.inc');

在目录“include”中我说:

在 init.inc 文件中:

require ("workbench.inc"); 

但突然不再起作用了。有一次我还必须在“init.inc”中指定目录。 为什么一直上班,突然就不行了?

【问题讨论】:

  • 你必须更明确一点。您是在 Apache 上还是在 CLI 上使用 PHP?你的目录结构是什么?
  • 我使用 Eclipse 并使用 XDebug。该应用程序已配置并且作为 CLI 应用程序运行,但我也安装了 wamp 并且 wamp 服务器仍在运行。目录结构非常简单。我有一个根目录和一个包含目录。
  • 当我将代码放入“include/init.inc”时: set_include_path(get_include_path() . PATH_SEPARATOR . 'D:\...my path ...\include');然后它工作正常。但是直到今天它都没有必要......

标签: php path include


【解决方案1】:

来自PHP documentation:

根据给定的文件路径包含文件,如果没有给出,则 指定的 include_path。如果文件在 include_path, include 最终会检入调用脚本自己的 失败前的目录和当前工作目录

所以current working directory (CWD) 在包含其他文件时很重要。可以通过getcwd()检索。

如果你的目录结构是这样的

rootdir/
    include/
        init.rc
        workbench.rc
    index.php

只有当rootdir/ 是 CWD 或者是搜索路径的一部分时,require('include/init.rc') 才会起作用。同样,require('init.inc') 假定 CWD 为 rootdir/include/。由于当前工作目录可以更改,因此在 PHP 中使用更健壮的是惯用的

// in index.php
require(__DIR__ . '/include/init.rc');

// in workbench.rc
require(__DIR__ . '/init.rc');

然后require 将独立于 CWD 工作。这是因为magic constant __DIR__ 被替换为包含不带尾随目录分隔符的常量的文件的绝对路径,例如

  • index.php__DIR__D:\path\to\rootdir
  • include/init.rc 中,__DIR__D:\path\to\rootdir\include

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-15
    • 1970-01-01
    • 2020-05-12
    • 2014-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多