require_once in php

試著忘記壹切 提交于 2019-12-11 07:00:37

问题


I have a php file which has a require_once Statement (?) this file is then in included in 2 other php files, one php file is in a sub directory so the layout is like this ("file1" and "file2" include the file "included" which require_onces the "required")#

 L--subfolder1
 | L--file1
 L--subfolder2
 | L--required
 L--file2
 L--included

How can I reference the "required" file from the "included" file so that it will work from both file1 and file2?


回答1:


always use absolute path

$_SERVER['DOCUMENT_ROOT']."/included";

or

$_SERVER['DOCUMENT_ROOT']."/subfolder2/required";

would work from anywhere




回答2:


You have to use absolute path to be able to require the file with the same code from anywhere.

require_once($_SERVER['DOCUMENT_ROOT'] . '/subfolder2/required');



回答3:


You can use dirname() in combination with the __FILE__ constant. (If I understand you correctly you're including the file 'included' from both scripts and 'included' then includes some files?)
included(.php):

require_once( dirname(__FILE__)."/subfolder2/required" );


来源:https://stackoverflow.com/questions/2652439/require-once-in-php

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