Skip to main content

Converting Between Relative and Absolute File Path

To convert a relative path to an absolute file path, use PHP’s realpath() function. This function performs “path math” means translating all the relative locations in a path string to return a complete absolute path.

If we pass “.” as parameter in below example

  1. <?php
  2. $abs_path = realpath(“.”);
  3. echo $abs_path;
  4. ?>

It will result absolute path as below:

“usr/local/apache/htdocs”

If we pass “..” as parameter then

  1. <?php
  2. $abs_path = realpath(“..”);
  3. echo $abs_path;
  4. ?>

It will result absolute path as below:

“usr/local/apache/”

PHPZAG.COM is a popular PHP tutorial blog that publishes useful tips and tutorials for PHP programmer. If you would like to be kept up to date with our posts, you can subscribe to our RSS Feed

Leave a Reply

Your email address will not be published. Required fields are marked *