PHP: Hypertext Preprocessor (original) (raw)
dirname
(PHP 4, PHP 5, PHP 7, PHP 8)
dirname — Returns a parent directory's path
Description
Note:
dirname() operates naively on the input string, and is not aware of the actual filesystem, or path components such as "
..
".
Caution
On Windows, dirname() assumes the currently set codepage, so for it to see the correct directory name with multibyte character paths, the matching codepage must be set. If path
contains characters which are invalid for the current codepage, the behavior of dirname() is undefined.
On other systems, dirname() assumes path
to be encoded in an ASCII compatible encoding. Otherwise, the behavior of the function is undefined.
Parameters
path
A path.
On Windows, both slash (/
) and backslash (\
) are used as directory separator character. In other environments, it is the forward slash (/
).
levels
The number of parent directories to go up.
This must be an integer greater than 0.
Return Values
Returns the path of a parent directory. If there are no slashes inpath
, a dot ('.
') is returned, indicating the current directory. Otherwise, the returned string ispath
with any trailing/component
removed.
Caution
Be careful when using this function in a loop that can reach the top-level directory as this can result in an infinite loop.
<?php dirname('.'); // Will return '.'. dirname('/'); // Will return `\` on Windows and '/' on *nix systems. dirname('\\'); // Will return `\` on Windows and '.' on *nix systems. dirname('C:\\'); // Will return 'C:\' on Windows and '.' on *nix systems. ?>
Examples
Example #1 dirname() example
<?php echo dirname("/etc/passwd") . PHP_EOL; echo dirname("/etc/") . PHP_EOL; echo dirname(".") . PHP_EOL; echo dirname("C:\\") . PHP_EOL; echo dirname("/usr/local/lib", 2);
The above example will output something similar to:
/etc
/ (or \ on Windows)
.
C:
/usr
See Also
- basename() - Returns trailing name component of path
- pathinfo() - Returns information about a file path
- realpath() - Returns canonicalized absolute pathname
Found A Problem?
y dot a dot dejong at singular dot of dot alumni dot utwente dot nl ¶
10 years ago
As of PHP 5.3.0, you can use __DIR__ as a replacement for dirname(__FILE__)
19 years ago
`Since the paths in the examples given only have two parts (e.g. "/etc/passwd") it is not obvious whether dirname returns the single path element of the parent directory or whether it returns the whole path up to and including the parent directory. From experimentation it appears to be the latter.
e.g.
dirname('/usr/local/magic/bin');
returns '/usr/local/magic' and not just 'magic'
Also it is not immediately obvious that dirname effectively returns the parent directory of the last item of the path regardless of whether the last item is a directory or a file. (i.e. one might think that if the path given was a directory then dirname would return the entire original path since that is a directory name.)
Further the presense of a directory separator at the end of the path does not necessarily indicate that last item of the path is a directory, and so
dirname('/usr/local/magic/bin/'); #note final '/'
would return the same result as in my example above.
In short this seems to be more of a string manipulation function that strips off the last non-null file or directory element off of a path string.
`
23 years ago
`To get the directory of current included file:
For example, if a script called 'database.init.php' which is included from anywhere on the filesystem wants to include the script 'database.class.php', which lays in the same directory, you can use:
`
6 years ago
Be aware that if you call dirname(__FILE__) on Windows, you may get backslashes. If you then try to use str_replace() or preg_replace() to replace part of the path using forward slashes in your search pattern, there will be no match. You can normalize paths with <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>p</mi><mi>a</mi><mi>t</mi><mi>h</mi><mo>=</mo><mi>s</mi><mi>t</mi><msub><mi>r</mi><mi>r</mi></msub><mi>e</mi><mi>p</mi><mi>l</mi><mi>a</mi><mi>c</mi><mi>e</mi><msup><mo stretchy="false">(</mo><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup><msup><mspace linebreak="newline"></mspace><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup><msup><mo separator="true">,</mo><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup><msup><mi mathvariant="normal">/</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup><mo separator="true">,</mo></mrow><annotation encoding="application/x-tex">path = str_replace('\\', '/' ,</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">p</span><span class="mord mathnormal">a</span><span class="mord mathnormal">t</span><span class="mord mathnormal">h</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1.0019em;vertical-align:-0.25em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1514em;"><span style="top:-2.55em;margin-left:-0.0278em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight" style="margin-right:0.02778em;">r</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal">e</span><span class="mord mathnormal" style="margin-right:0.01968em;">pl</span><span class="mord mathnormal">a</span><span class="mord mathnormal">ce</span><span class="mopen"><span class="mopen">(</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.7519em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">′</span></span></span></span></span></span></span></span></span><span class="mord"><span class="mspace newline"></span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.7519em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">′</span></span></span></span></span></span></span></span></span><span class="mpunct"><span class="mpunct">,</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.7519em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">′</span></span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord"><span class="mord">/</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.7519em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">′</span></span></span></span></span></span></span></span></span><span class="mpunct">,</span></span></span></span>path) before doing any transformations
joe dot naylor at gmail dot com ¶
16 years ago
`The dirname function does not usually return a slash on the end, which might encourage you to create links using code like this: url=dirname(url = dirname(url=dirname(_SERVER['PHP_SELF']) . '/somepage.php';
However dirname returns a slash if the path you specify is the root, so $url in that case would become '//somepage.php'. If you put that URL as the action on a form, for example, submitting the form will try to go to http://somepage.php.
I ran into this when I wrote a site on a url with a path, www.somehost.com/client/somepage.php, where the code above works great, but then wanted to put it on a subdomain, client.somehost.com/somepage.php, where things started breaking.
The best solution would be to create a function that generates absolute URLs and use that throughout the site, but creating a safe_dirname function (and an htaccess rewrite to fix double-slashes just in case) fixed the issue for me:
dirname=dirname(dirname = dirname(dirname=dirname(path); return dirname==′/′?′′:dirname == '/' ? '' : dirname==′/′?′′:dirname; } ?>`
klugg this-is-junk at tlen dot pl ¶
19 years ago
`Attention with this. Dirname likes to mess with the slashes.
On Windows, Apache:
'; ?>
prints out
$_SERVER[PHP_SELF]: /index.php
Dirname($_SERVER[PHP_SELF]: \
`