Hi Niels,

On Wed, 6 Dec 2023 at 19:20, Niels Dossche <dossche.ni...@gmail.com> wrote:

> Hi internals
>
> I'd like to start a pre-RFC discussion about filesystem path APIs in PHP.
> The reason I bring this up is because of this recent feature request:
> https://github.com/php/php-src/issues/11258
>
> The feature request is about the following:
> We already have some functions to work with paths in PHP, e.g. basename,
> dirname, realpath.
> We do not have some common path APIs that you can find in other languages
> like: path_join to join paths and path_normalize to normalize paths.
>
> As not everyone may be familiar with such functions, I'll explain them
> briefly.
>
> **Proposed Functions:**
>
> 1. path_join(string... $components): string;
> This function concatenates the components with a / (or \ on Windows)
> between them and normalizes the path.
> If the resulting path is empty then it'll return "." to indicate the
> current directory.
> You may be wondering "Isn't this just implode?". Not really, it normalizes
> the path too, which means that components like "." are removed from the
> path, redundant slashes are removed, and if there are ".." components then
> they remove the previous component from the path.
>
> It's also not the same thing as calling realpath after doing implode:
> realpath would not work for non-existent paths and it would allow escaping
> from the root.
> To demo the root escape problem:
> path_join("..", "foo"); yields "foo"
> Whereas realpath("../foo"); yields the absolute path of the "foo" file one
> directory up.
>
> Note: This function does not do any I/O.
>
> 2. path_normalize(string $path): string;
>
> This function only does the normalization part described above.
> Note: This function also does not do any I/O.
>
> **Examples:**
>
> ```php
> // Example usage of path_join
> $result = path_join('dir1', 'dir2', '..', 'file.txt'); // Resolves to
> 'dir1/file.txt'
>
> // Example usage of path_normalize
> $normalizedPath = path_normalize('dir1/../dir2'); // Resolves to 'dir2'
> ```
>
>
> I think these would be great additions to PHP as working with paths and
> files is a core part of any programming language.
>
>
> Seems like it, is there an argument to be made to, let's say, in the
performance side ?

Reply via email to