php – Difference between './', '../' and '/'

Question:

In some codes I work with, sometimes there isn't a global variable that points to the project's root, 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 access to resources from the local directory?

Example:

|a.php -> inclui ('pasta/b.php') 
|pasta/b.php -> acessa ('img.png')
|pasta/img.png

Answer:

Each of these changes the way a directory is referenced.

Let's assume a teste.txt file:

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

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

  • ../teste.txt : it means that the file teste.txt 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 in relation to the application's root directory, and not in relation to the system , to avoid confusion. With this you guarantee portability to the application, which can run on any system or directory structure, regardless of its file system.

Scroll to Top