I was curious if ../
and /../
is the same in PHP so I tried these:
require_once('/../frame/header.php');
require_once('../frame/header.php');
Both of them worked. Was I just lucky or they realy do the same exact thing? Why?
In your context if they both worked, that implies that your scripts are at the root of the filesystem you have access to, or one level in. They are not the same however! /../
refers to the filesystem root (and ..
one directory up, which just gets eaten and is still the root), while ../
refers to one directory higher than the current one. Any path beginning with /
is an absolute path from the filesystem root.
From anywhere other than the filesystem root, these would not function equivalently.
Suppose your working directory is /var/www/scripts
.
require_once('../include.php');
Will include a file at /var/www/include.php
.
But from that same location, if you did
require_once('/../include.php');
...php will attempt to load the file /include.php
at the filesystem root, and it probably won't exist.
Now, a lot of web hosts will supply you with a filesystem whose root /
is also the web server's document root, the web server document root is only one directory level in from the root like /www
. In that case, /../
may work fine, but beware if you ever attempt to move it to another server with a different filesystem configuration.
So if the script's working directory was /www
, just by luck, these two would function the same way:
require_once('../include.php');
require_once('/../include.php');
Both would include the file /include.php
at the filesystem root.
Note, this is not PHP-specific, but a property of filesystems which use the ..
in general...