PHP require_once or include_once nesting

Directory structure

  • A.php
  • folder
    • B.php
    • C.php

A.php includes B.php and B.php includes C.php.

But B.php needs to include C.php relative to A.php. Is there a way to include C.php relative to B.php?

+4
source share
2 answers

You can enable the use of __DIR__ or dirname(__FILE__) .

From the manual

File directory. If used inside a include, the directory returns the returned file. This is equivalent to dirname(__FILE__) .

+3
source

try it

A.php

 include_once(__DIR__.'/folder/B.php'); include_once(__DIR__.'/folder/C.php'); 

B.php

 include_once(__DIR__.'/A.php'); 
+1
source

All Articles