Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Absolute path & Relative Path

What's the difference between absolute path & relative path when using any web server or Tomcat?

like image 991
Phani Kumar Bhamidipati Avatar asked Oct 08 '08 08:10

Phani Kumar Bhamidipati


People also ask

What is absolute and relative path?

An absolute path is defined as specifying the location of a file or directory from the root directory(/). In other words,we can say that an absolute path is a complete path from start of actual file system from / directory. Relative path. Relative path is defined as the path related to the present working directly(pwd) ...

Is an absolute path or relative path?

A relative path describes the location of a file relative to the current (working) directory*. An absolute path describes the location from the root directory. When learning to access data files through programming, we regularly use relative file paths.

What is an absolute path in Terminal?

/A/B/file is an absolute path. An absolute path is in truth relative to the filesystem root directory / . No matter the context, no matter what terminal you paste this path into, it will always specify the same path and so the same file. A/B/file is a relative path.


2 Answers

Absolute paths start with / and refer to a location from the root of the current site (or virtual host).

Relative paths do not start with / and refer to a location from the actual location of the document the reference is made.

Examples, assuming root is http://foo.com/site/

Absolute path, no matter where we are on the site

/foo.html

will refer to http://foo.com/site/foo.html

Relative path, assuming the containing link is located in http://foo.com/site/part1/bar.html

../part2/quux.html

will refer to http://foo.com/site/part2/quux.html

or

part2/blue.html

will refer to http://foo.com/site/part1/part2/blue.html

like image 186
Vinko Vrsalovic Avatar answered Jan 27 '23 12:01

Vinko Vrsalovic


Important to note that relative paths are also subjective.

ie:

<?php 
  #bar.php
  require('../foo.php'); 
?>
/dir/bar.php 
/foo.php         # prints a 
/dir/foo.php # prints b 
/dir/other/   # empty dir
$ pwd 
>  /
$ php dir/bar.php 
>  / + ../foo.php == /foo.php   
>  prints a 
$ cd dir 
$ php bar.php
>  /dir  + ../foo.php = /foo.php 
>  prints a
$ cd other
$ php ../bar.php 
> /dir/other + ../foo.php  = /dir/foo.php 
> prints b

This can create some rather confusing situations, especially if you have many files with releative references and multiple possible places that can act as an "entry point" that controls what the relative path is relative to.

In such situations, one should compute the absolute path manually based on a fixed known, ie:

<?php
    require( realpath(dirname(__FILE__) . '/../foo.php') )

or

<?php
   require( SOMECONSTANT . '/relative/path.php'  ); 

or

<?php
   require( $_SERVER['DOCUMENT_ROOT'] . '/relative/path.php' );
like image 43
Kent Fredric Avatar answered Jan 27 '23 14:01

Kent Fredric