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

Asked

Viewed 9,925 times

25

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

$RAIZ='PATH/EXAMPLE/';

So they often use:

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

What’s the difference?

What happens to files from other directories that have been included (include) 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
  • 2

    A tip off friend, to avoid problems with inclusions of different locations, it is best to put the whole way in the includes. As for example define('FULL_PATH', dirname($_SERVER['SCRIPT_FILENAME']) . '/'); include FULL_PATH . 'pasta/meu-arquivo.php';. Use this define only in a file in the root folder of your project, to access is "constant", include it in a global file and include this file in other :)

3 answers

24


Each of these changes the way a directory is referenced.

Suppose a file teste.txt:

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

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

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

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

  • 1

    Your last sentence defines everything, in the application always having the absolute path can help prevent many headaches +1

16

Having the folder structure down:

Estrutura de pastas modelo

Considering I’m in the file conteúdo.html and want to include the archive horas.html, as the two are on the same level folders so I should use:

include "./horas.html";

To include the archive dicas.html that’s in the folder recursos I shall use:

include "./recursos/dicas.html";

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

Now to include the file catálogo.html that’s in the folder produtos I shall use:

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

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

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

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

So

In src='./somePath'; the ./ means you are referring to the current folder

In src='../somePath'; the ../ means that you are referencing a folder prior to the current one. You can use several to search in folders at different levels.
Example:
src='../../../otherPath'; here I am looking for the folder otherPath in a folder that is 3 levels above the folder where the current script is running.

In src='/somePath'; 0 / means that you are referring to the system root folder

-5

A simple way:

Consider Root Folder as Dirzero...

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

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

Browser other questions tagged

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