Require_once in php

I have a php file with the require_once (?) Instruction, this file is then included in 2 other php files, one php file is in a subdirectory, so the layout is like this ("file1" and "file2" include the file "included" for which required "required") #

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

How can I refer to the “necessary” file from the “included” file so that it works with both files 1 and file2?

+1
source share
3 answers

always use the absolute path

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

or

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

will work anywhere

+5
source

You must use an absolute path to be able to request a file with the same code from anywhere.

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

You can use dirname () in combination with the __FILE__ constant. (If I understand you correctly, do you include the file “included” from both scripts and “included” and then contains some files?)
enabled (.php):

 require_once( dirname(__FILE__)."/subfolder2/required" ); 
+4
source

Source: https://habr.com/ru/post/910845/


All Articles