doubt with include php

1

Well I have a function where I need to load an include file into the function. Like this:

function Query($conexao, $query) {

   // Includes
   include "../Dados.php";

}

The problem is that in some files in which I call the function it does not find the data file. This is because some files are inside some folders. Is there any way to do the include fetch the data file in the root of the site? That is regardless of where I call the function it goes in the root and looks for the file.

    
asked by anonymous 06.03.2017 / 17:14

1 answer

2

You can use __DIR__ of predefined

** example *:

function Query($conexao, $query) {

   // Includes
   include __DIR__ . "../Dados.php";

}

__DIR__ will point from the directory where the script of the function is.

editing :

The following function is based on the root and not relative to the directory as in the example above:

function Query($conexao, $query) {
    // includes
    include  str_replace("\", "/", dirname($_SERVER["DOCUMENT_ROOT"])."/path/or/subpath/to/Dados.php");
}

The above function can be used to direct the path to the Dados.php file from the root.

A local example in Windows would look something like this:

echo str_replace("\", "/", dirname($_SERVER["DOCUMENT_ROOT"])."/path/or/subpath/to/Dados.php");

// output: C:/Server/www/path/or/subpath/to/Dados.php
    
06.03.2017 / 17:34