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

Asked

Viewed 2,897 times

0

I have to navigate inside my site with a menu, and I created a file with all the code of the menu, and in the files q I wanted the menu I gave a require_once ""; Only, not all files are in the same index directory And I want a method that returns the necessary directory to reach the index

Example: I have a folder called Folder and inside this folder has a file called file.php, in this file I used require_once to add the menu, and there has to be a method inside the menu file.php q returned the directory to make the "link" with the index.php files, about.php In case you would have q return ".. /index.php" to have access to it

You can do it?

2 answers

0

$_SERVER['PHP_SELF'] returns the name of the script file it is running, on the root of the document.

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 above script for the URL http://www.exemplo.com/estoque/produto.php?id=5 will produce ../produto.php

If the menu files are in 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 and so forth.

The variable $relative resolves to index or any other file that is in the root, and its application to any other folder. However, it cannot be applied to other menu links if they are in other folders. In this case use a/ bar that you specify 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>

0


Consider the folder and file structure:

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

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

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

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

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

What you need to know is that always the way / will point to the directory root. From it you can define the path you want. This way, the contents of the archive 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>
  • Thank you so much Anderson for the explanation !

Browser other questions tagged

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