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/”

Leave a Reply

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

Shares

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!