PHP - directory path

0

I'm making a website that has a column called -News - , this column I call through include . (you can imagine that it is an external file - news.php) , so far normal.

It turns out that on my site there are folders on other levels, for example:

  • index.php
  • news.php
  • content / action / index.php
  • content / material / index.php
  • content / util / index.php

There are more or less 10 folders with the same structure.

On the main page ( index.php ), I am including the news.php as follows:

$path = "news.php" ; 
include $path;

So far as normal.

It happens that when I call the same file in index that has sublevels, my URL loses the reference.

$path = "../../news.php" ; 
include $path;

The news field appears on my pages, but when I click on a link the URL appears wrong. Here's an example below:

content/news/2018/acao/news/2018/index.php

I would like to know if there is something, which makes PHP , of the subfolders, return to the parent level (lets say so).

I tried to use $path = $_SERVER['DOCUMENT_ROOT']; , but it did not work. dirname($path) did not work either.

Of course PHP is correct in your reading, but if you can help me solve this problem, I'll be grateful.

    
asked by anonymous 17.04.2018 / 21:42

1 answer

1

You can do it this way:

$raiz = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR;
include $raiz . "news.php";

So you always get the site host and you do not have to worry about which page is called include .

If you're going to use include for other files that are inside a folder, do so:

$raiz = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR;
include $raiz . "content/util/index.php";

So you need to think about the path from the root.

I did the following to make things easier:

function caminho($path){
    $path = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . $path;
    $barras = array('\', '/');
    return str_replace($barras,DIRECTORY_SEPARATOR,$path);
}
include caminho("news.php");
    
17.04.2018 / 21:53