Get relative path to the directory where the index.php file is located

0

I have to do the navigation inside my site with a menu, and I created a file with all the menu code, and in the files I wanted the menu I gave a "require_once"; So, not all files are in the same index directory And I want a method that returns the required directory to get to index

Example: I have a folder named folder and inside this folder there is a file called file.php, in that file I used require_once to add the menu, and it has to have a method inside the menu.php file that returns the directory to do the "linkage "with the index.php files, about.php In case you would have to return "../index.php" to access it

Can you do this?

    
asked by anonymous 05.05.2017 / 23:15

2 answers

0

Consider the structure of folders and files:

public_html/
    folder/
        file.php
        menu.php
    index.php
    sobre.php
    contato.php

The public_html directory will be the root of your application. You can create any link relative to this directory by starting the URL with a / slash. The link below will always correctly point to the index.php file inside the root directory.

<a href="/index.php">Início</a>

In this way it is independent of which file is displaying the menu, the link will be the same. If you need to link files in subfolders, just do:

<a href="/folder/file.php">Arquivo</a>

What you need to know is that the / path will always point to the root directory. From it you can set the path you want. In this way, the content of the folder/menu.php would be something like:

<ul>
  <li><a href="/index.php">Início</a></li>
  <li><a href="/sobre.php">Sobre nós</a></li>
  <li><a href="/contato.php">Contato</a></li>
</ul>
    
06.05.2017 / 02:07
0

$ _SERVER ['PHP_SELF'] returns the file name of the script that is running,   relative to the document root .

  

Example: Suppose you are accessing the URL http://www.exemplo.com/estoque/produto.php?id=5 then $ _SERVER ['PHP_SELF'] returns "/estoque/produto.php" in your script.

Script to use relative path with notation ../

 $caminho = ($_SERVER['PHP_SELF']);
 $quant = substr_count($caminho, '/');
 $quant=$quant-1;
 for ($k = 0; $k < $quant; $k++) {
   $relativo.="../";
 }
 echo "<a href=\"".$relativo."index.php\">index</a>";

The script above for the URL http://www.exemplo.com/estoque/produto.php?id=5 will produce ../produto.php

If the menu files are the same:

   <a href=\"".$relativo."index.php\">Início</a>
   <a href=\"".$relativo."demo.php">Demo</a>
   <a href=\"".$relativo."chat.php">Chat</a>

Directory 1 level above, will produce

   <a href="../index.php\">Início</a>
   <a href="../demo.php">Demo</a>
   <a href="../chat.php">Chat</a>

Directory 2 levels above, will produce

   <a href="../../index.php\">Início</a>
   <a href="../../demo.php">Demo</a>
   <a href="../../chat.php">Chat</a>

And so on.

  

The variable $ relative resolves to the index or any other file that is in the root, and its application in any other folder. However, it can not be applied to other menu links if they are in other folders. In this case use a / slash that indicates from the root.

<a href="../index.php">Início</a>
<a href="/pastaA/pastaB/demo.php">demo</a>
<a href="/pastaB/chat.php">chat</a>
    
06.05.2017 / 01:53