【问题标题】:Including scripts from another directory in php?在 php 中包含来自另一个目录的脚本?
【发布时间】:2014-02-11 16:07:33
【问题描述】:

在我的 php 项目中包含一些脚本时,我遇到了一个非常具体的问题。

如果我的 main directory 中有一个 php 脚本,main directory 中的 second directory 中有另一个脚本,我可以使用以下命令从第一个脚本中调用它:

include 'secondDirectory/myScript.php';\

我现在拥有的是我的main directory,里面有 3 个目录让我们说:

Directory1
Directory2
HelpFunctions

Directory1Directory2 中的一些脚本也使用了来自 HelpFunctions 的脚本,所以我需要包含它们。

我尝试的是像这样包含它们:

include '../HelpFunctions/myScript.php';

这意味着从Directory1Directory2返回main directory,然后转到HelpFunctions并包含myScript.php

但是我总是收到这样的警告:

Warning: include(../HelpFunctions/scrapeFunction.php): failed to open stream: No such file or directory in

【问题讨论】:

  • 您包含一个包含,因此您需要使其相对于位于 mainDirectory 中的文件或使用绝对路径
  • 1:检查大小写,*nix 系统区分大小写。 2:尝试 realpath() uk3.php.net/realpath__DIR__ 常量,例如include realpath( __DIR__ . '/../HelpFunctions/myScript.php' );

标签: php path include


【解决方案1】:

你的问题可能是位置文件

但你可以试试这个

$root = realpath($_SERVER["DOCUMENT_ROOT"]);

include "$root/HelpFunctions/myScript.php";

【讨论】:

    猜你喜欢
    • 2013-05-03
    • 1970-01-01
    • 2014-03-17
    • 1970-01-01
    • 2011-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多