php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #45279 Results of dirname() on paths ending with a /
Submitted: 2008-06-16 10:59 UTC Modified: 2008-11-05 11:18 UTC
Votes:2
Avg. Score:4.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: ralph at deboom dot biz Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: 5.2.6 OS: Debian Etch 4.0+
Private report: No CVE-ID: None
 [2008-06-16 10:59 UTC] ralph at deboom dot biz
Description:
------------
Quoted from: http://bugs.php.net/bug.php?id=841
[21 Dec 1998 2:06pm UTC] rasmus

The correct operations of these functions should be...

dirname returns everything PRIOR to the last / or \ in the path.
basename returns everything AFTER the last / or \ in the path.
----------------------------------------------------------------
There are several more people on www.php.net/dirname comments who are confused by this aswell.

Reproduce code:
---------------
<?php
echo dirname('/dir1/dir2/dir3/');

Expected result:
----------------
/dir1/dir2/dir3

Or/And update the docs: www.php.net/dirname

Actual result:
--------------
/dir1/dir2


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-06-20 08:04 UTC] ralph at deboom dot biz
Where is the support on this bug...?
 [2008-06-20 08:37 UTC] pajoye@php.net
It may be a BC break to change the behavior now so I would prefer to update the documentation instead. However, after having looked at the code, it seems to be a bug in the implementation (but the BC issue remains).

Assigned to Dmitry (on request).
 [2008-06-20 11:12 UTC] ralph at deboom dot biz
Ok super! :)

-------------------------------
I got a workaround for the people who are using the older versions:

<?php
function __dirname($dirname) {
	if (substr($dirname, -1, 1) == '/' || substr($dirname, -1, 1) 
== '\\') {
		$dirname = dirname($dirname.'.');
	}
	else {
		$dirname = dirname($dirname);
	}
}
 [2008-06-20 11:13 UTC] ralph at deboom dot biz
~_~ Oops, forgot the return $dirname:
-------------------------
<?php
function __dirname($dirname) {
	if (substr($dirname, -1, 1) == '/' || substr($dirname, -1, 1) 
== '\\') {
		$dirname = dirname($dirname.'.');
	}
	else {
		$dirname = dirname($dirname);
	}
	return $dirname;
}
 [2008-08-01 14:42 UTC] dmitry@php.net
The current behavior is consistent and it won't be changed, as it may break existent code.
 [2008-08-01 17:17 UTC] ralph at deboom dot biz
Then please do update the docs?
 [2008-08-01 17:21 UTC] pajoye@php.net
Marked as "Won't fix" from a dev point of view, move to "Documentation"
 [2008-11-05 11:18 UTC] vrana@php.net
dirname() and basename() clearly states that it accepts a path to a file, not a directory.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Dec 19 14:00:02 2025 UTC