0

I am working on a PHP website where I needed to get a list of the files in the Images directory. The example in PHP.net shows the addressing with just a forward slash as shown in the example. This addressing pattern did not work. Instead the address needs to start with ./ . What is the difference?

<?php
$dir    = '/tmp';
$files1 = scandir($dir);
$files2 = scandir($dir, 1);

print_r($files1);
print_r($files2);
?>

A search for other questions have addressed single dot and double dot, but I don't see posts for a forward slash with no dot.

3 Answers3

2

the . indicates the current directory. Assume, for instance, that your current path is /var/www:

If you say ./tmp the directory you are looking for is /var/www/tmp

If you say /tmp you are looking for /tmp (starting from the root)

2

Maybe you're a Windows user and you are not familiar with the way Linux addresses files, but essentially there are no drive letters. / is the root of the file system, basically equivalent to C:\ on Windows (though not quite). It also works on Windows except it will refer to the root of the partition your script is running in as Windows separates each partition on a different drive letter.

There's a standard folder in the root of the file system called tmp, and if you want to refer to it you want to specify full path, /tmp.

Just using tmp or ./tmp will refer to a tmp folder on your local path, which is a completely different folder just with the same name (that might not even exist).

When you don't want to specify a full path but somewhere relative or local path instead, you don't put / in the beginning. You might just put nothing, or you can put ./ to explicitate this is a path relative to your current working directory which is ..

It doesn't matter in which folder your script working, . always represents it. It is actually unnecessary to put ./ in most cases since relative paths are implicit.

On a relate note, .. represents the parent folder.

Example, my script is /var/www/script.php, when I run it:

  • . is the folder /var/www.
  • fopen('note.txt', 'r') will open file /var/www/note.txt for reading.
  • fopen('./note.txt', 'r') will do the exact same thing.
  • .. is the folder /var.
  • ../../tmp is the same as /tmp.

Note the current working directory represented by . remains constant even if you include() a script from a subfolder.

This might get things confusing because . may not be the folder the script you included is in. To workaround this problem, use __DIR__ to get the folder your script is in, it will get the current folder of your script even if you are calling it through a include() or calling it from a different directory.

Havenard
  • 27,022
  • 5
  • 36
  • 62
  • 2
    `.` is *not* the path the script is in. It is the current working directory. Unless someone or something changes it, this will be the path the script is ran *from*, not the one it is in. – Jörg W Mittag Sep 23 '18 at 22:09
-1
  • / denotes the root directory of your server. As in /var/www/.
  • ./ denotes the current directory that the script is executing in. This is the equivalent of getcwd(). For example, you're currently in /php/scripts, it will refer to /var/www/php/scripts.
  • ../ denotes the parent folder, and can be chained to move up multiple levels.

The use of each depends on which file or folder you're trying to manipulate from which script.

If you're trying to access /var/www/tmp from /var/www/php/scripts/script.php, you can use either:

  • ../../tmp
  • /tmp

Note that if you're unsure how many levels you need to 'move up', you can affix an infinite number of ../; it won't 'break out' of the root. Having said that, referencing directly from the root is often much more straight-forward.

Obsidian Age
  • 41,205
  • 10
  • 48
  • 71