Lisa Miskovsky
Lisa Miskovsky

Reputation: 926

Pathing using ./, ../, .. - what do they exactly mean for my view files?

I always worry about pathing issues when I move my website into subfolders because I don't know how to handle it with PHP.

I want to learn this with an example because I can easily learn it by looking at examples.

Let's say I have a website running at this directory: httpdocs/development/subfolder/myWebsite/index.php

By default, PHP would run httpdocs/index.php, so it is the root path but my website runs in a subfolder.

In my website's index.php, (so we're in a subfolder) how can I ensure I point correct folders?

<img src="images/1.jpg"> // Does no dot means root path?

<img src="./images/1.jpg"> //Does ./ means which_directory_our_php_page_currently_in/images?

<a href="./"> // Points /subfolder/myWebsite/ or httpdocs/ ?

<a href=".."> //Same as above, but no front slash.

<a href=""> //Same as above

I don't want to make a definition or a const to track it with PHP and change it whenever I move my files. Like:

define('SITE_PATH', './development/subfolder/myWebsite/');

Especially when it comes into DIRECTORY_SEPARATOR, things only get more confusing.

I would like to know how to handle it with PHP professionally; what is the difference between and ./; lastly what does .. mean without forward slash.

Thank you.

Upvotes: 0

Views: 136

Answers (2)

nbrooks
nbrooks

Reputation: 18233

In UNIX systems, './dirName' looks for a sub-directory dirName in the current directory; . simply refers to the current location. In other words, ./dirName is equivalent to 'dirName'. You don't use that in an href though: for paths relative to the current location, do not use the preceding ..

'../dirName' looks for a sub-directory dirName in the parent of the current directory. .. refers to the parent directory.

If the filepath begins with a forward slash, it is referring to the root directory. '/dirName' refers to a sub-directory dirName located inside the root directory.

Upvotes: 1

Richard Brown
Richard Brown

Reputation: 11436

All the paths in your examples are relative, meaning they are based off of the current location. Starting a path with a / means it's an absolute path, based on the root of the site.

If you want to always be 100% sure of what you're referencing use the / at the front of your paths.

Upvotes: 2

Related Questions