PHP require() relative path error

时光毁灭记忆、已成空白 提交于 2019-12-02 20:02:27

Try adding dirname(__FILE__) before the path, like:

require(dirname(__FILE__).'/../../dir2/file3.php');

It should include the file starting from the root directory

SharpC

For relative paths you can use __DIR__ directly rather than dirname(__FILE__) (as long as you are using PHP 5.3.0 and above):

require(__DIR__.'/../../dir2/file3.php');

Remember to add the additional forward slash at the beginning of the path within quotes.

See:

A proper advice here, is to never ever use such things as "../../" relative paths in your web apps. It's hard to read and terrible for maintenance.

  1. As you can attest. It makes it extremely difficult to know what you are pointing to.

  2. If you need to change the folder level of your application, or parts of it. It's completely prone to errors, and will likely break something that is horrible to debug.

Instead, define a few constants in your bootstrap file for your main path(s) and then use:

require(MY_DIR.'/dir2/file3.php');

Moving your app from there, is as easy as replacing your MY_DIR constants in one single file.

You can always use the $_SERVER['DOCUMENT_ROOT'] as a valid starting point, as well, rather than resorting to a relative path. Just another option.

require($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');

I think your cwd is dir2. Try :

require("file3.php");
require("file4.php");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!