include an php file with included files

2019-04-02 02:01发布

Here is directory structure

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

Here is codes in these files

/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');

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:

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

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

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

How can I fix this issue?

标签: php include
1条回答
做自己的国王
2楼-- · 2019-04-02 02:44

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 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 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.

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

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.

查看更多
登录 后发表回答