php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #48659 dirname() to accept path level to return
Submitted: 2009-06-23 10:47 UTC Modified: 2014-04-30 22:43 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: ninzya at inbox dot lv Assigned:
Status: Wont fix Package: Filesystem function related
PHP Version: Any OS: Irrelevant
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
17 + 40 = ?
Subscribe to this entry?

 
 [2009-06-23 10:47 UTC] ninzya at inbox dot lv
Description:
------------
It would be great if dirname() function had a second parameter, let's say - uint $level, which would mean "how many path levels to strip off". By default this parameter could be set to 1 to not to break the backwards compatibility.

Reproduce code:
---------------
<?php

$path ='/var/www/localhost/public_html/tpl/';

echo dirname( $path);// /var/www/localhost/public_html
echo dirname( $path, 2);// /var/www/localhost
echo dirname( $path, 3);// /var/www/
echo dirname( $path, 4);// /var
echo dirname( $path, 5);// .
echo dirname( $path, 6);// .

?>

Expected result:
----------------
Expect this feature to be implemented

Actual result:
--------------
-

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-07-13 15:56 UTC] ajfweb at googlemail dot com
This is ancient, but this should be closed. It's completely unnecessary, .. 
exists.
 [2014-04-30 22:43 UTC] levim@php.net
-Status: Open +Status: Wont fix -Package: Feature/Change Request +Package: Filesystem function related -Operating System: - +Operating System: Irrelevant -PHP Version: 5.3.0RC4 +PHP Version: Any
 [2014-05-01 10:21 UTC] ninzya at inbox dot lv
Quote from dirname() docs:

dirname() operates naively on the input string, and is not aware of the actual filesystem, or path components such as "..".

Therefore, dirname( "a/b/c/.." ) will yield "a/b/c" which is not what I want.

>> It's completely unnecessary
Wouldn't say that it is COMPLETELY unnecessary. Otherwise, why do we have dirname() in first place? We have "..", right?

One of valid use cases is to keep paths without ".." bloat.
 [2014-05-01 10:30 UTC] ajf at ajf dot me
Oops, you're right. However, if you want to make it strip multiple levels, you can simply apply the function multiple times, or write a function to apply it so:

function dirname_multilevel($path, $level = 1) {
    return $level ? dirname_mutilevel(dirname($path), $level - 1) : $path;
}
 [2014-05-01 10:37 UTC] ninzya at inbox dot lv
Yep, agree. That's what I do already. Just thought maybe implementing this feature in dirname() natively would make things better.

Thanks anyway. Not a critical problem!
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 10 06:01:31 2024 UTC