PHP - with require_once/include/require, the path is relative to what?

倾然丶 夕夏残阳落幕 提交于 2019-12-17 19:52:34

问题


when on "index.php", I do require_once("/all_fns.php").

"all_fns.php" itself requires files with paths relative to all_fns.php (itself).

My question is, should I write the paths on all_fns relative to all_fns.php or to index.php?

This is all very confusing to me and wanted to get it straight.


回答1:


They are treated as relative to index.php.

If you need to reference a file relative to the include, use

__DIR__."/relative/file.php";



回答2:


They are relative to the getcwd().




回答3:


in index.php define some directories

define ( 'INCLUDES_DIR', dirname( __FILE__ ) );
define ( 'INCLUDES_DIR2', INCLUDES_DIR . '/some_directory' );

then in all the other files you can use INCLUDES_DIR

include( INCLUDES_DIR1 . 'file.php' );



回答4:


Either Use

echo  in_get( "include_path") ;

Or open up your ini file and find out what your ini path is, and put your include file in THAT DIRECTORY (or one of those directories) and release yourself from worrying about absolute/relative include problems forever.



来源:https://stackoverflow.com/questions/1954750/php-with-require-once-include-require-the-path-is-relative-to-what

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