PHP - Relative paths “require”

删除回忆录丶 提交于 2019-11-29 06:43:24

If you find that relative include paths aren't working as expected, a quick fix is to prepend __DIR__ to the front of the path you're trying to include.

require __DIR__ . "/../blog.php";

It's reasonably clean, and you don't need to modify the include path or working directory.

If you are including this files db.php and functions.php in index.php then you have to write this code

require "../db.php";
require "../functions.php";

OR if you are including this files in blog.php then write this code

require "db.php";
require "functions.php";

You need to set the include_path in your php.ini.

If you want to set it at run-time, use set_include_path().

I like to start my files with chdir($_SERVER['DOCUMENT_ROOT']). This allows me to get a nice and logical base path for all my includes.

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