Difference between './', '../' and '/'

23

In some of the codes I work, sometimes there is no global variable that points to the root of the project, such as:

$RAIZ='PATH/EXAMPLE/';

So often they use:

src='./somePath';
src='../somePath';
src='/somePath';

What's the difference?

What happens to files from other directories that have been included and need to access resources from the local directory?

Example:

|a.php -> inclui ('pasta/b.php') 
|pasta/b.php -> acessa ('img.png')
|pasta/img.png
    
asked by anonymous 28.04.2015 / 17:30

3 answers

22

Each of these changes the way a directory is referenced.

Assume a teste.txt file:

  • /teste.txt : means that the teste.txt file is in the system root folder;

  • ./teste.txt : means that the teste.txt file is in the same folder as the script is running;

  • ../teste.txt : means that the teste.txt file is in the folder immediately above the folder where the PHP script is running.

In any programming language, it is always important to reference files relative to the root application , rather than the system , to avoid confusion. This ensures portability to the application, which can run on any system or directory structure, regardless of its file system.

    
28.04.2015 / 17:33
16

Having the folder structure down:

ConsideringthatIaminthefileconteúdo.htmlandwanttoincludethefilehoras.html,sincebothareinthesamefolderlevel,soIshoulduse:

include"./horas.html";

To include the dicas.html file that is in the recursos folder, I will use:

include "./recursos/dicas.html";

Since the recursos folder is at the same level as conteúdo.html

Now to include the file catálogo.html that is in the produtos folder I should use:

include "../produtos/catálogo.html";

So I will go up one level between the folders and from there enter the path of the desired file.

But knowing that the produtos folder is a folder that is in the root folder I can use:

include "/produtos/catálogo.html";

Thus

In src='./somePath'; ./ means that you are referencing the current folder

In% with% of% with% means that you are referencing a folder that is earlier than the current one. You can use several to search folders on different levels.
Example: src='../somePath'; here I'm looking for the ../ folder in a folder that is 3 levels above the folder where the current script is running.

In src='../../../otherPath'; 0 otherPath means that you are referencing the system root folder

    
28.04.2015 / 17:37
-5

A simple way:

Consider Root Folder as DirZero ...

In the file being worked (independent of the Directory), and will include the arquivoincluido.php that is two levels below the root:

include ($_SERVER['DOCUMENT_ROOT'].'/DirNivel1/DirNivel2/arquivoincluir.php');
    
17.01.2017 / 02:49