Q:

Include php files when they\'re in different folders (directories)

belongs to collection: PHP Miscellaneous

0

Assume that we have the following directory structure:

Document Root

  • DirectoryA
    • file1.php
    • file2.php
    • file3.php
    • file4.php
    • file5.php
  • DirectoryB
    • index.php

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Now, Assuming that we want to call file(1-5).php in index.php which is in DirectoryB, we can use the following approach:

Contents of index.php:

<?php
include($_SERVER['DOCUMENT_ROOT'].'/../DirectoryA/file1.php');
include($_SERVER['DOCUMENT_ROOT'].'/../DirectoryA/file2.php');
include($_SERVER['DOCUMENT_ROOT'].'/../DirectoryA/file3.php');
include($_SERVER['DOCUMENT_ROOT'].'/../DirectoryA/file4.php');
include($_SERVER['DOCUMENT_ROOT'].'/../DirectoryA/file5.php');
?>

In the above example, $_SERVER gets the DOCUMENT_ROOT asset in the configuration for the given site and then performs actions relative to it, i.e.: /../ takes to the parent folder and then we add the relative path to file.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

PHP Miscellaneous

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Import a CSS file in php without using HTML format... >>
<< Include a Class from another file in PHP...