php require_once 没有按照我想要的方式工作..相对路径问题

发布于 2024-08-21 06:20:38 字数 584 浏览 2 评论 0原文

我在为 require_once 分配相对路径时遇到问题。我确信这很简单,我没有看到......

文件夹目录结构 级别 1:站点 2 级:包括 2级:类

... site/include/global.php <-- 这是调用自动加载函数的地方 site/class/db.php

当我尝试使用时,

function__autoload($className)
{
     require_once '../class/'.$className.'.php';

}

我得到:

警告:require_once(../class/db.php) [function.require-once]: 无法打开流:没有这样的文件或目录

致命错误:require_once () [function.require]: 无法打开必需的 '../class/db.php' (include_path='.;./includes;./pear')

我做错了什么?如果我将 global.php 放在类文件夹中,它会工作得很好,所以我假设是我对相对路径的理解不佳导致了问题。

谢谢

I'm having problems assigning a relative path to require_once. I'm sure it's something simple that I'm not seeing...

folder directory structure
level 1: site
level 2: include
level 2: class

so...
site/include/global.php <-- this is where function autoload is being called from
site/class/db.php

when I attempt to use

function__autoload($className)
{
     require_once '../class/'.$className.'.php';

}

i get:

Warning: require_once(../class/db.php) [function.require-once]: failed to open stream: No such file or directory

Fatal error: require_once() [function.require]: Failed opening required '../class/db.php' (include_path='.;./includes;./pear')

What am I doing wrong? It'll work fine if I plop the global.php in the class folder so I'm assuming it's my poor understanding of relative paths that is causing the problem..

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠檬 2024-08-28 06:20:38

require(_once)include(_once) 相对于第一个脚本的路径工作,即实际调用的脚本

如果您需要 require 包含或必需的文件中的某些内容,并且希望相对于该文件进行工作,请使用

dirname(__FILE__)."/path/to/file";

或 正如@Arkh 指出的那样,从 PHP 5.3 开始

__DIR__."/path/to/file";

require(_once) and include(_once) work relative to the path of the first script, i.e. the script that was actually called.

If you need to require something in an included or required file, and would like to work relative to that file, use

dirname(__FILE__)."/path/to/file";

or as @Arkh points out, from PHP 5.3 upwards

__DIR__."/path/to/file";
无声情话 2024-08-28 06:20:38

这行得通吗(仅限 apache,我相信

require_once($_SERVER['DOCUMENT_ROOT'] . '/class/' . $classname '.php');

Does this work (apache only, i believe)

require_once($_SERVER['DOCUMENT_ROOT'] . '/class/' . $classname '.php');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文