rampuriyaaa
rampuriyaaa

Reputation: 5126

Computation of relative path

I am facing difficulty in understanding relative path concept, I have seen a part of code written as

../../abc/file/images/picutre/down.gif

how the relative path is computed

Upvotes: 0

Views: 78

Answers (5)

Jako Basson
Jako Basson

Reputation: 1531

A relative path is a path relative to the working directory. In other words the starting point to look for files is from the working directory.

The "../" in a relative path means to go up one directory.

So lets say you're referencing the relative path ../../abc/file/images/picutre/down.gif from an index.html page in the following structure :

http://someexampleurl.com/dir1/dir2/index.html

Your working directory when working from index.html is /dir2 so taking into account that you're going up two levels, the browser expects the file to be at:

 http://someexampleurl.com/abc/file/images/picutre/down.gif

Upvotes: 1

Leandro Papasidero
Leandro Papasidero

Reputation: 3738

how the relative path is computed

Basically a relative path is a "map" from the directory that you are located to the file you need to include. Therefore, relative path is computed based on where you want to go.

For example you have a structure

/ (document root)
|--home.php
|--t.php
|--common
      |--header.php
      |--footer.php
|--support
      |--index1.php
|--privacy
|     |--index2.php

From home.php you need to include header and footer. Therefore your home code will look like

<?php
include("common/header.php"); // go one folder down (common) and grab the file header.php
include("common/footer.php"); // go one folder down (common) and grab the file footer.php

Now let say you are in index1.php in support and you need header.php and footer.php. You code will look like

<?php
include("../common/header.php"); // go one folder up (common) and grab the file header.php
include("../common/footer.php"); // go one folder up (common) and grab the file footer.php

Think folder inside folder as levels (level1, level2, etc)

Note: Be careful with relative paths something they are a pain.

Upvotes: 1

Ramesh Murugesan
Ramesh Murugesan

Reputation: 5013

  1. down.gif is present in the same directory
  2. / starts form root directory
  3. ../ one directory back from current directory
  4. ../../ two directory back from current directory

Upvotes: 0

winhowes
winhowes

Reputation: 8065

So if we are on https://example.com/my/path/here and it loaded a file ../../abc/file/images/picutre/down.gif then we would go up 2 directories because of the 2 ../'s to https://example.com/my. Then we would go down to /abc/file/images/picutre/down.gif. So the final destination would be https://example.com/my/abc/file/images/picutre/down.gif

Upvotes: 0

user1627167
user1627167

Reputation: 339

it says go back up two level (parent directory) "../../" from current location.

Upvotes: 0

Related Questions