Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dirname() produces invalid root path #8095

Closed
mabar opened this issue Feb 14, 2022 · 1 comment
Closed

dirname() produces invalid root path #8095

mabar opened this issue Feb 14, 2022 · 1 comment

Comments

@mabar
Copy link

mabar commented Feb 14, 2022

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

@bwoebi
Copy link
Member

bwoebi commented Feb 14, 2022

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()).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants