Sol Sol - 3 months ago 11
PHP Question

For changing file folders in php what is the difference between /../ ./../ and ../?

For changing file folders what is the difference between

/../

./../

../ ?

Some includes won't work unless it is

include "/../filename.php";
while others will work just fine with
include "../filename.php;"
.

Answer

Pathing 101:

Every running process has a "working directory". This is where any "relative" paths in file operations will be based from.

./          - current directory (the "working" directory)
../         - parent directory
../..       - grandparent directory
../foo      - brother/sister directory "foo"
../../foo   - uncle/aunt directory "foo"

There are also ABSOLUTE paths, which start at the ultimate ancestor of all directories - the root of the file system. On Unix-ish systems, that's /, on Windows systems, that'd be a drive letter, e.g. C:\

/            - ultimate starting directory - root of file system
/foo         - immediate child directory
/foo/bar     - grandchild directory
/foo/bar/baz - great-grandchild directory.

Combining these:

absolute+relative:

/foo/bar/../baz

   1. `/`     - start at root /
   2. `/foo`  - descend into 'foo' (now at `/foo`)
   3. `/bar`  - descent into 'bar' (now at `/foo/bar`)
   4. '../`   - go back a level  (back at `/foo`)
   5  `/baz   - descend into  'baz` (now at `/foo/baz`)

relative:

../foo/bar/../baz

  1. Start in current directory `./`
  2. `../` - ascend to parent (now at `../`)
  3. `foo/` - descend into sibling `foo` (now at `../foo`)
  4. `bar/` - descend into niece/nephew `bar` (now at `../foo/bar`)
  5. `../` - ascend back to sibling foo (now at `../foo`)
  6. `baz/` - descend into niece/nephew 'bar' (now at `../foo/baz`)

Basically, ANY path which starts with / or c:\ is an absolute path, and any path starting with a . is a relative one. EVERY directory contains . and .., which are simply pointers to itself (.), and its parent (..). The parent is there so you don't need to know the name of your ancestor directory, you just need to know ...

Since directories contain a reference to themselves, something like ./././././. is simply a very redundant way of saying "in the current directory".

Therefore ../foo/./bar is no different than ../foo/bar, because the ., being where it is, simply means "stay in the same place".

Comments