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.