Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8439733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:04:57+00:00 2026-06-10T08:04:57+00:00

PHP has a nice realpath() function, which can convert something like /dir1/dir2/../dir3/filename to /dir1/dir3/filename

  • 0

PHP has a nice realpath() function, which can convert something like /dir1/dir2/../dir3/filename to /dir1/dir3/filename. The “problem” with this function is that in case /dir1/dir3/filename is not an actual file but merely a link to another file, PHP would follow that link and return the real path of the actual file.

However, I actually need to get the real path of the link itself. All I need is to resolve complexities like /dir/.. in the path. How can I do it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T08:04:59+00:00Added an answer on June 10, 2026 at 8:04 am

    I was hoping to find an existing PHP function that does that, or else something simple along the lines of xdazz’s answer (but that would actually work the way I want it to). Having failed to find such an answer, I’ve written my own dirty function. I’d be happy to hear your comments and suggestions for improvement!

    // return the contracted path (e.g. '/dir1/dir2/../dir3/filename' => '/dir1/dir3/filename')
    // $path: an absolute or relative path
    // $rel: the base $path is given relatively to - if $path is a relative path; NULL would take the current working directory as base
    // return: the contracted path, or FALSE if $path is invalid
    function contract_path($path, $rel = NULL) {
      if($path == '') return FALSE;
      if($path == '/') return '/';
      if($path[strlen($path) - 1] == '/') $path = substr($path, 0, strlen($path) - 1); // strip trailing slash
      if($path[0] != '/') { // if $path is a relative path
        if(is_null($rel)) $rel = getcwd();
        if($rel[strlen($rel) - 1] != '/') $rel .= '/';
        $path = $rel . $path;
      }
      $comps = explode('/', substr($path, 1)); // strip initial slash and extract path components
      $res = Array();
      foreach($comps as $comp) {
        if($comp == '') return FALSE; // double slash - invalid path
        if($comp == '..') {
          if(count($res) == 0) return FALSE; // parent directory of root - invalid path
          array_pop($res);
        }
        elseif($comp != '.') $res[] = $comp;
      }
      return '/' . implode('/', $res);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

PHP has a is_readable function which checks to see if the file is readable
Python has a nice zip() function. Is there a PHP equivalent?
PHP has a very nice function, isset($variableName). It checks if $variableName is already defined
PHP has a function called extract() which takes an associative array as the argument
I have a eval function like this if(FALSE === @eval($code)) echo 'your code has
the question is very straightforward: Good DAL library for PHP which has the similar
Reading all about PHP frameworks, CodeIgniter seems really nice in that I like it's
The PHP tidy extension has a function ob_tidyhandler Docs that works with PHP output
I have a config.php file which has some constants and methods. I have a
There is this really nice function from the php.net documentation that enables you to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.