문제

I am using a php library which has this code: require_once dirname(__FILE__) . '/config.php';

From what I've read, dirname(__FILE__) points to the current directory.

So wouldn't it be easier to just write require_once 'config.php';?

My only guess here is that including the dirname(__FILE__) ensures that the require_once function uses an absolute rather than relative path.

도움이 되었습니까?

해결책

Yes, you are right - dirname(__FILE__) ensures that the require_once function uses an absolute rather than relative path.

The __FILE__ constant represents the running script. It will return the full path and file name of the running script.

For example, if a script called database.init.php which is included from anywhere on the filesystem wants to include the script database.class.php, which lays in the same directory, you can use:

require_once dirname(__FILE__) . '/database.class.php'; 

다른 팁

an example:

if your file is inlcuded somewhere then by default it will start searching from the path of the file that included yours. in this case the require will not work for files that are next to your file

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top