Absolute path & Relative Path

旧巷老猫 提交于 2019-11-26 08:30:30

问题


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


回答1:


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




回答2:


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' );



回答3:


Through trial and error I have determined that the starting point of a path in Tomcat is the webapps folder.

In other words if your Java code is trying to read ../somefile.txt then the absolute path to that file would be %TOMCAT_HOME%/webapps/../somefile.txt i.e. %TOMCAT_HOME%/webapps/somefile.txt



来源:https://stackoverflow.com/questions/181805/absolute-path-relative-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!