Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121936 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 90547 invoked from network); 6 Dec 2023 19:20:35 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 6 Dec 2023 19:20:35 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id B321F180003 for ; Wed, 6 Dec 2023 11:20:44 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-ed1-f53.google.com (mail-ed1-f53.google.com [209.85.208.53]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 6 Dec 2023 11:20:44 -0800 (PST) Received: by mail-ed1-f53.google.com with SMTP id 4fb4d7f45d1cf-548f853fc9eso173973a12.1 for ; Wed, 06 Dec 2023 11:20:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701890430; x=1702495230; darn=lists.php.net; h=content-transfer-encoding:subject:from:to:content-language :user-agent:mime-version:date:message-id:from:to:cc:subject:date :message-id:reply-to; bh=4wcHd9EuIfScqGKIDnFgxYSODIHD2hMypLg+XsXojbA=; b=R35GwQ4E/J84bEad9amPBjPP9usyte2whekF//XG31nv56kJibXM5MOhC5fp0S+Cmw dAHsJ5l2zhDBGWHy45PabdMstWmcHt2tXeTvqLBgkIbv7cgfnz8HLEHjpc02TcNs15Cx jRgbyoasvNr053TB3fKA4tSLltf580ZtxVudQKzRwcv+lBuaFow65HQYL/pw4LpOeyb4 gLl6BdYzk4AJwcH3fOokWwdbFln6N6e6rYWdp8SA/TSd1MS1xnRBKCZh2k+8nGokHIEc oFXZXaO7+4XMuWln5rA0SO4W4GHvqD2IMpsKa6zlGjAMae9uKlPdZeNAnnQ3K/WsOACK isEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701890430; x=1702495230; h=content-transfer-encoding:subject:from:to:content-language :user-agent:mime-version:date:message-id:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=4wcHd9EuIfScqGKIDnFgxYSODIHD2hMypLg+XsXojbA=; b=DOrNCgo9HLDDvmx8UPu1d5nwmATv0Q1HK7gbKf4KsFshvCZmtZcDLs9OHTh8NtM3E9 BA/aOapkznUxGuCMctSBpOCZLnuPlr6nsCHmRYfIlYIx8Rb1y2ZZNYbD1mSoVc/HErKa 7NqNHtow2+hHm1Ab0w14Pb9AW8DG0oDqS17KIaMoAXNNlLF0fO3ysTsK1mK51A4jZJyl ucVGuMmHJR+9TT9Mc68YJ8z0xtCu+2pqazb03++CAqrdLKRbK/Y5yGVz9nbtqpa/8T4r QJAbFsFdVXhEmpFMmar0aGJ5CwEtMU7pY/nxsRefFdL3241hypb4z4JIrCHhHGkAC0mA DG8Q== X-Gm-Message-State: AOJu0Yz3sl9rnWG08sOFBs4jyJF/XIX3df0MjXOYeJDPfe4NdY4O8WEK AkaZsxT8BbQXB2h8AJ6Onej5LzTYjOw= X-Google-Smtp-Source: AGHT+IEgWVH+BqudqAdG36odbKwu9CJORbhN1aKn62hFxvWKGZaV7eYHbdw7ynT/w9sNrmBJy/9HvQ== X-Received: by 2002:a17:906:4e49:b0:a1c:84b4:5fc with SMTP id g9-20020a1709064e4900b00a1c84b405fcmr491572ejw.22.1701890430295; Wed, 06 Dec 2023 11:20:30 -0800 (PST) Received: from ?IPV6:2a02:1811:cc83:ee50:280e:1e36:3a00:824? (ptr-dtfv08akcem5xburtic.18120a2.ip6.access.telenet.be. [2a02:1811:cc83:ee50:280e:1e36:3a00:824]) by smtp.gmail.com with ESMTPSA id mp25-20020a1709071b1900b00a1d32dfacb9sm320032ejc.38.2023.12.06.11.20.29 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 06 Dec 2023 11:20:29 -0800 (PST) Message-ID: <8f4e3274-6489-4378-82db-fb4829770196@gmail.com> Date: Wed, 6 Dec 2023 20:20:29 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US To: PHP internals Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Subject: Filesystem path APIs From: dossche.niels@gmail.com (Niels Dossche) 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. Furthermore, languages like Python and runtimes like Node.JS allow to join and normalize paths that are not native to the operating system PHP is running on. E.g. it's possible to create a Posix-style path (what is used on Linux, macOS, BSD etc) on a Windows system. This is sometimes necessary in order to construct a path to access a remote file on a Linux server to just give an example. In PHP we could add functions like (just to give an example): \Path\Windows\join for Windows paths \Path\Posix\join for Linux/macOS/Posix paths \Path\join that automatically chooses one of the above depending on what operating system PHP runs on. Similar approach for path_normalize. There could be OOP-style alternatives too, e.g. Rust has a PathBuf struct with methods that are used to build paths. However if we were to choose this route then we need to be aware that interoperability with existing filesystem functions would be much harder because they all work directly with strings at the moment. What do you think? Kind regards Niels