包含一个包含文件的php文件 [英] include an php file with included files

查看:153
本文介绍了包含一个包含文件的php文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是目录结构


  • /global.php

  • /includes/class_bootstrap.php

  • /includes/init.php

  • /plugins/myplugin.php

  • /global.php
  • /includes/class_bootstrap.php
  • /includes/init.php
  • /plugins/myplugin.php

/start.php

require('./includes/class_bootstrap.php');

/includes/class_bootstrap.php

define('CWD', (($getcwd = getcwd()) ? $getcwd : '.'));
require_once(CWD . '/includes/init.php');

/plugins/myplugin.php

require_once(dirname(__FILE__).'../global.php');

据我所知,问题出现在class_bootstrap.php文件中,因为它产生错误的路径CWD
这里是错误:

And as far as I am understanding the problem is in class_bootstrap.php file coz it's generating wrong path for CWD here is error:


警告:require_once(C:/ wamp / www / vb4 / plugins / includes / init。 php)[function.require-once]:
无法打开流:第35行的C:/wamp/www/vb4/global.php中没有这样的文件或目录

Warning: require_once(C:/wamp/www/vb4/plugins/includes/init.php) [function.require-once]: failed to open stream: No such file or directory in C:/wamp/www/vb4/global.php on line 35

正如你所看到的那样C:/wamp/www/vb4/plugins/includes/init.php是错误的路径。

As you can see "C:/wamp/www/vb4/plugins/includes/init.php" is wrong path.

主要问题是我只能编辑myplugin.php文件,其他文件是CMS核心文件,不应更改。

The MAIN PROBLEM is that I can edit only myplugin.php file other files are CMS core files and should not be changed.

如何解决此问题?

推荐答案

如果您需要确定一组脚本的基本路径,不应该依赖当前的工作目录。这可以从执行环境变为执行环境。

If you need to determine the base path of a set of scripts, you should not rely on the "current working directory." This can change from executing environment to executing environment.

相反,将其基于已知路径。

Instead, base it on a known path.

/ includes / class_bootstrap。 php 知道它将是基本路径所在的目录,所以它可以这样做:

/includes/class_bootstrap.php knows that it's going to be one directory down from where the base path is going to be, so it can do this:

define('CWD', realpath(dirname(__FILE__) . '/../') );

dirname 获取传递的字符串中给出的目录名称。如果 __ FILE __ 返回 C:/wamp/www/vb4/plugins/includes/class_bootstrap.php ,则 dirname 将返回 C:/ wamp / www / vb4 / plugins / includes 。然后我们将 /../ 附加到它,然后调用 realpath ,将相对 .. 转换为真实目录: C:/ wamp / www / vb4 / plugins

dirname gets the directory name given in the passed string. If __FILE__ returns C:/wamp/www/vb4/plugins/includes/class_bootstrap.php, then dirname will return C:/wamp/www/vb4/plugins/includes. We then append /../ to it and then call realpath, which turns that relative .. into a real directory: C:/wamp/www/vb4/plugins

Phew。

从那时起, CWD 将按预期运行。您可以 require_once CWD。 '/includes/init.php',它将正确解析为 C:/wamp/www/vb4/plugins/includes/init.php

From that point forward, CWD will operate as you expect. You can require_once CWD . '/includes/init.php' and it will correctly resolve to C:/wamp/www/vb4/plugins/includes/init.php

此外,这可能听起来很愚蠢,但vb4可能指的是vBulletin 4,在这种情况下,您的插件可能已经可以访问它公开的配置信息,包括路径等方便的东西。这可能使整个练习变得不必要。我故意对vB一无所知,否则我会指向你的开发文档。

Also, this may sound stupid but "vb4" may be referring to vBulletin 4, in which case your plugin may already have access to the configuration information that it exposes, including handy things like paths. This may make this entire exercise unnecessary. I intentionally know nothing about vB, otherwise I would point you at their dev docs.

这篇关于包含一个包含文件的php文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆