PHP - with require_once/include/require, the path

2020-02-06 03:14发布

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.

标签: php path
4条回答
▲ chillily
2楼-- · 2020-02-06 03:41

They are treated as relative to index.php.

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

__DIR__."/relative/file.php";
查看更多
欢心
3楼-- · 2020-02-06 03:54

They are relative to the getcwd().

查看更多
Fickle 薄情
4楼-- · 2020-02-06 03:54

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.

查看更多
乱世女痞
5楼-- · 2020-02-06 04:00

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' );
查看更多
登录 后发表回答