Doubt about include in files in previous directories

Asked

Viewed 58 times

0

Let’s say I have a file called header.php located in CMS/admin/includes. Let’s say I gave include this header.php file in a file called index.php located in CMS/admin.

Inside the header.php file I want to give include in a file called functions.php that is in CMS/admin (the same directory as index.php). I should use:

<?php include "../functions.php" ?>

or just

<?php include "functions.php" ?>

because header.php is being included in a file (index.php) that is found in the same functions.php directory (CMS/admin), I should give include as if functions.php were in the same header.php directory?

I could tell?

  • If Voce used index.php, the other includes, the index location must be referenced. Do you understand? type if the file that is the main one and will receive the other files, it will become only one. Then it must be refocused from the same location

  • Depending on the case it is best to take a fixed starting point and concatenate, such as the $_SERVER['DOCUMENT_ROOT'] . 'restodocaminho' if you have too much depth variation. Or, set up the PHP includes path for a default folder. http://php.net/manual/en/ini.core.php#ini.include-path . Ideally it pays to organize in a way that does not depend on any of this..

1 answer

0

The current directory of the first, index.php, is assumed in all other files.

Can only use <?php include "functions.php" ?>.

However, be aware of the configuration directive include_path.

To find out what it contains in that Directive, you can invoke the function ini_get().

echo ini_get('include_path');

It is important to know this because if an unforeseen event happens, an accident and you have another "functions.php" file in another directory that is in "include_path", you can load an unexpected file.

It is safer to adopt the use of absolute paths.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.