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 6182577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:13:19+00:00 2026-05-24T01:13:19+00:00

Is there a PHP internal function for path concatenation ? What possibilities do I

  • 0

Is there a PHP internal function for path concatenation ? What possibilities do I have to merge several paths (absolute and relative).

//Example: 
$path1="/usr/home/username/www";
$path2="domainname";
$path3="images/thumbnails";
$domain="exampledomain.com";

//As example: Now I want to create a new path (domain + path3) on the fly. 
$result = $domain.DIRECTORY_SEPARATOR.$path3

Ok, there is an easy solution for this example, but what if there are different dictionary separators or some paths are a little bit more complicated?

Is there an existing solution for trim it like this: /home/uploads/../uploads/tmp => /home/uploads/tmp ….

And how would a platform-independent version of an path-concat-function look like?

should an relative path start with “./” as prefix or is “home/path/img/” the common way?

  • 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-05-24T01:13:19+00:00Added an answer on May 24, 2026 at 1:13 am

    I ran into this problem myself, primarily regarding the normalization of paths.

    Normalization is:

    • One separator (I’ve chosen to support, but never return a backwards slash \\)
    • Resolving indirection: /../
    • Removing duplicate separators: /home/www/uploads//file.ext
    • Always remove trailing separator.

    I’ve written a function that achieves this. I don’t have access to that code right now, but it’s also not that hard to write it yourself.

    Whether a path is absolute or not doesn’t really matter for the implementation of this normalization function, just watch out for the leading separator and you’re good.

    I’m not too worried about OS dependence. Both Windows and Linux PHP understand / so for the sake of simplicity I’m just always using that – but I guess it doesn’t really matter what separator you use.


    To answer your question: path concatenation can be very easy if you just always use / and assume that a directory has no trailing separator. ‘no trailing separator’ seems like a good assumption because functions like dirname remove the trailing separator.

    Then it’s always safe to do: $dir . "/" . $file.

    And even if the result path is /home/uploads/../uploads//my_uploads/myfile.ext it’s still going to work fine.

    Normalization becomes useful when you need to store the path somewhere. And because you have this normalization function you can make these assumptions.


    An additional useful function is a function to make relative paths.

    • /files/uploads
    • /files/uploads/my_uploads/myfile.ext

    It can be useful to derive from those two paths, what the relative path to the file is.


    realpath

    I’ve found realpath to be extremely performance heavy. It’s not so bad if you’re calling it once but if you’re doing it in a loop somewhere you get a pretty big hit. Keep in mind that each realpath call is a call to the filesystem as well. Also, it will simply return false if you pass in something silly, I’d rather have it throw an Exception.

    To me the realpath function is a good example of a BAD function because it does two things: 1. It normalizes the path and 2. it checks if the path exists. Both of these functions are useful of course but they must be separated. It also doesn’t distinguish between files and directories. For windows this typically isn’t a problem, but for Linux it can be.

    And I think there is some quirky-ness when using realpath("") on Windows. I think it will return \\ – which can be profoundly unacceptable.


    /**
     * This function is a proper replacement for realpath
     * It will _only_ normalize the path and resolve indirections (.. and .)
     * Normalization includes:
     * - directiory separator is always /
     * - there is never a trailing directory separator
     * @param  $path
     * @return String
     */
    function normalize_path($path) {
        $parts = preg_split(":[\\\/]:", $path); // split on known directory separators
    
        // resolve relative paths
        for ($i = 0; $i < count($parts); $i +=1) {
            if ($parts[$i] === "..") {          // resolve ..
                if ($i === 0) {
                    throw new Exception("Cannot resolve path, path seems invalid: `" . $path . "`");
                }
                unset($parts[$i - 1]);
                unset($parts[$i]);
                $parts = array_values($parts);
                $i -= 2;
            } else if ($parts[$i] === ".") {    // resolve .
                unset($parts[$i]);
                $parts = array_values($parts);
                $i -= 1;
            }
            if ($i > 0 && $parts[$i] === "") {  // remove empty parts
                unset($parts[$i]);
                $parts = array_values($parts);
            }
        }
        return implode("/", $parts);
    }
    
    /**
     * Removes base path from longer path. The resulting path will never contain a leading directory separator
     * Base path must occur in longer path
     * Paths will be normalized
     * @throws Exception
     * @param  $base_path
     * @param  $longer_path
     * @return string normalized relative path
     */
    function make_relative_path($base_path, $longer_path) {
        $base_path = normalize_path($base_path);
        $longer_path = normalize_path($longer_path);
        if (0 !== strpos($longer_path, $base_path)) {
            throw new Exception("Can not make relative path, base path does not occur at 0 in longer path: `" . $base_path . "`, `" . $longer_path . "`");
        }
        return substr($longer_path, strlen($base_path) + 1);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there some equivalent of friend or internal in php? If not, is there
Is there a Php function to determine if a string consist of only ASCII
In PHP 5.2 there was a nice security function added called input_filter, so instead
What I'd like I'd like to have an internal web app in PHP where
I know about the unwanted behaviour of PHP's function strtotime For example, when adding
Is there a PHP class/library that would allow me to query an XHTML document
I there a PHP based source control 'server' that is compatible with SVN clients?
I'm a rookie designer having a few troubles with this page: http://www.resolvegroup.co.nz/javasurvey.php There are
Are there any decent PHP libraries available for accessing SVN repositories? Right now I
I had made some questions regarding PHP-GTK (there are only 4 php-gtk tagged questions

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.