dirname() produces invalid root path
Description
The following code:
<?php
echo dirname('file://file.txt');
Resulted in this output:
file:
But I expected this output instead:
file://
https://3v4l.org/6FlUN
While it may not be problem with real filesystem where writing to root directory is a no-go, it's quite useful in tests for writing via custom stream wrapper into a php variable which exists just for that test. Following code used to create directories hierarchy before writing into file due to the problem creates directory vfs.var: in current working directory instead of (not) creating root directory.
$file = 'vfs.var://file.txt';
$dir = dirname($file);
if (!@mkdir($dir, $mode, true) && !is_dir($dir)) {
// exception
}
file_put_contents($file, '');
PHP Version
All
Operating System
No response
This is not a bug. dirname() is explicitely for paths, not uniform resource locators or identifiers.
A double slash in a path is simply simplified to a single slash. And, under Linux at least, a colon is a valid part of a path (though it is used as a path separator in some contexts, you can have a directory named file:).
PHP has no direct facilities for URI parsing (URL parsing is provided by parse_url()).