php require_once 没有按照我想要的方式工作..相对路径问题
我在为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
require(_once)
和include(_once)
相对于第一个脚本的路径工作,即实际调用的脚本。如果您需要
require
包含或必需的文件中的某些内容,并且希望相对于该文件进行工作,请使用或 正如@Arkh 指出的那样,从 PHP 5.3 开始
require(_once)
andinclude(_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, useor as @Arkh points out, from PHP 5.3 upwards
这行得通吗(仅限 apache,我相信)
Does this work (apache only, i believe)