<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.filesystem.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.dirname.php',
    1 => 'dirname',
    2 => 'Returns a parent directory\'s path',
  ),
  'up' => 
  array (
    0 => 'ref.filesystem.php',
    1 => 'Filesystem Functions',
  ),
  'prev' => 
  array (
    0 => 'function.delete.php',
    1 => 'delete',
  ),
  'next' => 
  array (
    0 => 'function.disk-free-space.php',
    1 => 'disk_free_space',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/filesystem/functions/dirname.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.dirname" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">dirname</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">dirname</span> &mdash; <span class="dc-title">Returns a parent directory&#039;s path</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.dirname-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>dirname</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$path</code></span>, <span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$levels</code><span class="initializer"> = 1</span></span>): <span class="type"><a href="language.types.string.php" class="type string">string</a></span></div>

  <p class="para rdfs-comment">
   Given a string containing the path of a file or directory, this function
   will return the parent directory&#039;s path that is
   <code class="parameter">levels</code> up from the current directory.
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    <span class="function"><strong>dirname()</strong></span> operates naively on the input string,
    and is not aware of the actual filesystem, or path components such
    as &quot;<code class="literal">..</code>&quot;.
   </p>
  </p></blockquote>
  <div class="caution"><strong class="caution">Caution</strong>
   <p class="para">
    On Windows, <span class="function"><strong>dirname()</strong></span> 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 <code class="parameter">path</code> contains characters which are invalid for the
    current codepage, the behavior of <span class="function"><strong>dirname()</strong></span> is undefined.
   </p>
   <p class="para">
    On other systems, <span class="function"><strong>dirname()</strong></span> assumes <code class="parameter">path</code>
    to be encoded in an ASCII compatible encoding. Otherwise, the behavior of
    the function is undefined.
   </p>
  </div>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.dirname-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">path</code></dt>
     <dd>
      <p class="para">
       A path.
      </p>
      <p class="para">
       On Windows, both slash (<code class="literal">/</code>) and backslash
       (<code class="literal">\</code>) are used as directory separator character. In
       other environments, it is the forward slash (<code class="literal">/</code>).
      </p>
     </dd>
    
    
     <dt><code class="parameter">levels</code></dt>
     <dd>
      <p class="para">
       The number of parent directories to go up.
      </p>
      <p class="para">
       This must be an integer greater than 0.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.dirname-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns the path of a parent directory. If there are no slashes in
   <code class="parameter">path</code>, a dot (&#039;<code class="literal">.</code>&#039;) is returned,
   indicating the current directory. Otherwise, the returned string is
   <code class="parameter">path</code> with any trailing
   <code class="literal">/component</code> removed.
  </p>

  <div class="caution"><strong class="caution">Caution</strong>
   <p class="para">
    Be careful when using this function in a loop that can reach the
    top-level directory as this can result in an infinite loop.
    <div class="informalexample">
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />dirname</span><span style="color: #007700">(</span><span style="color: #DD0000">'.'</span><span style="color: #007700">);    </span><span style="color: #FF8000">// Will return '.'.<br /></span><span style="color: #0000BB">dirname</span><span style="color: #007700">(</span><span style="color: #DD0000">'/'</span><span style="color: #007700">);    </span><span style="color: #FF8000">// Will return `\` on Windows and '/' on *nix systems.<br /></span><span style="color: #0000BB">dirname</span><span style="color: #007700">(</span><span style="color: #DD0000">'\\'</span><span style="color: #007700">);   </span><span style="color: #FF8000">// Will return `\` on Windows and '.' on *nix systems.<br /></span><span style="color: #0000BB">dirname</span><span style="color: #007700">(</span><span style="color: #DD0000">'C:\\'</span><span style="color: #007700">); </span><span style="color: #FF8000">// Will return 'C:\' on Windows and '.' on *nix systems.<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
     </div>

    </div>
   </p>
  </div>
 </div>


 <div class="refsect1 examples" id="refsect1-function.dirname-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 <span class="function"><strong>dirname()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">dirname</span><span style="color: #007700">(</span><span style="color: #DD0000">"/etc/passwd"</span><span style="color: #007700">) . </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">dirname</span><span style="color: #007700">(</span><span style="color: #DD0000">"/etc/"</span><span style="color: #007700">) . </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">dirname</span><span style="color: #007700">(</span><span style="color: #DD0000">"."</span><span style="color: #007700">) . </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">dirname</span><span style="color: #007700">(</span><span style="color: #DD0000">"C:\\"</span><span style="color: #007700">) . </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">dirname</span><span style="color: #007700">(</span><span style="color: #DD0000">"/usr/local/lib"</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">);</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output
something similar to:</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">/etc
/ (or \ on Windows)
.
C:\
/usr</pre>
</div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.dirname-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.basename.php" class="function" rel="rdfs-seeAlso">basename()</a> - Returns trailing name component of path</span></li>
    <li><span class="function"><a href="function.pathinfo.php" class="function" rel="rdfs-seeAlso">pathinfo()</a> - Returns information about a file path</span></li>
    <li><span class="function"><a href="function.realpath.php" class="function" rel="rdfs-seeAlso">realpath()</a> - Returns canonicalized absolute pathname</span></li>
   </ul>
  </p>
 </div>


</div><?php manual_footer($setup); ?>