Current Location: Home> Latest Articles> Can dirname and pathinfo be used together? Examples of path and file information parsing

Can dirname and pathinfo be used together? Examples of path and file information parsing

gitbox 2025-08-05
<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-comment">// This part of the code is unrelated to the article content, just a demonstration example</span></span><span>
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"Welcome to this article!"</span></span><span>;
</span><span><span class="hljs-meta">?&gt;</span></span><span>
<p><hr></p>
<p></span><?php<br>
/**</p>
<ul>
<li>
<p>Can dirname and pathinfo be used together? Examples of path and file information parsing</p>
</li>
<li></li>
<li>
<p>In PHP, when handling file paths, dirname and pathinfo are both commonly used functions.</p>
</li>
<li>
<p>They are used to retrieve different parts of a path. This article explains their usage and whether they can be combined,</p>
</li>
<li>
<p>demonstrating how to parse path and file information with examples.<br>
*/</p>
</li>
</ul>
<p>/**</p>
<ul>
<li>
<p>dirname function</p>
</li>
<li></li>
<li>
<p>dirname() returns the parent directory path of the given path.</p>
</li>
<li>
<p>It can accept a second parameter to specify the number of levels to go up.<br>
*/<br>
$filepath = "/var/www/html/index.php";</p>
</li>
</ul>
<p>echo "Original path: " . $filepath . "\n";</p>
<p>// Get the immediate parent directory<br>
$parentDir = dirname($filepath);<br>
echo "Parent directory: " . $parentDir . "\n";</p>
<p>// Get the grandparent directory (two levels up)<br>
$grandParentDir = dirname($filepath, 2);<br>
echo "Grandparent directory: " . $grandParentDir . "\n\n";</p>
<p>/**</p>
<ul>
<li>
<p>pathinfo function</p>
</li>
<li></li>
<li>
<p>pathinfo() returns an array containing directory name, basename, extension, and filename (without extension) of a file path.</p>
</li>
<li>
<p>You can also retrieve a specific part by passing a second parameter.<br>
*/<br>
$pathInfo = pathinfo($filepath);</p>
</li>
</ul>
<p>echo "Structure of the array returned by pathinfo():\n";<br>
print_r($pathInfo);</p>
<p>// Get only the file extension<br>
$extension = pathinfo($filepath, PATHINFO_EXTENSION);<br>
echo "File extension: " . $extension . "\n";</p>
<p>// Get only the filename (without extension)<br>
$filename = pathinfo($filepath, PATHINFO_FILENAME);<br>
echo "Filename (without extension): " . $filename . "\n\n";</p>
<p>/**</p>
<ul>
<li>
<p>Can dirname and pathinfo be used together?</p>
</li>
<li></li>
<li>
<p>The answer is yes.</p>
</li>
<li>
<p>You can use dirname first to get the parent directory path, then use pathinfo to parse details of a higher-level path,</p>
</li>
<li>
<p>or combine both to handle different parts of the path separately.</p>
</li>
<li>
<p>For example, if you want to get the filename information of the parent directory of the file's directory, you can do this:<br>
*/</p>
</li>
</ul>
<p>$parentDir = dirname($filepath);<br>
echo "Directory of the file: $parentDir"\n";</p>
<p>// Parse the parent directory's path information<br>
$parentDirInfo = pathinfo($parentDir);<br>
echo "Parent directory path information:\n";<br>
print_r($parentDirInfo);</p>
<p>/**</p>
<ul data-is-last-node="" data-is-only-node="">
<li>
<p>Summary:</p>
</li>
<li>
<ul>
<li>
<p>dirname is used to get the parent directory part of a path, suitable for recursive upward directory traversal.</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>pathinfo is used to get detailed information like directory name, filename, and extension in a path.</p>
</li>
</ul>
</li>
<li data-is-last-node="">
<ul data-is-last-node="">
<li data-is-last-node="">
<p data-is-last-node="">Using both together provides more flexible parsing and handling of file paths.<br>
*/<br>
?><br>
</span>