Backend Development 5 min read

Understanding PHP dirname() Function with Examples

This article explains PHP's dirname() function, detailing its syntax, parameters, and return value, and demonstrates its usage through four practical code examples that show how to extract directory paths from absolute, relative, and Windows-style file paths, including handling edge cases.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Understanding PHP dirname() Function with Examples

PHP is a widely used scripting language for web development, providing many built‑in functions to help developers handle various tasks efficiently. One very useful function is dirname() . This article introduces the purpose of dirname() and provides code examples.

The dirname() function returns the directory portion of a given path. It extracts the directory name without the file name or trailing slash, which is useful for obtaining the directory of a file, especially when generating paths dynamically or handling file operations.

Syntax of dirname() :

<code>string dirname ( string $path [, int $levels = 1 ] )</code>

Parameters:

$path : required, the path string to be processed.

$levels : optional, the number of directory levels to return, default is 1.

Return value:

Returns the directory path as a string.

Usage of dirname()

Example 1:

<code>$path = "/var/www/html/myfile.txt";<br>$dir = dirname($path);<br>echo $dir;</code>

Result:

<code>/var/www/html</code>

In this example, the given path /var/www/html/myfile.txt is processed by dirname() , which extracts and returns the directory part /var/www/html .

Example 2:

<code>$path = "../img/pic.jpg";<br>$dir = dirname($path);<br>echo $dir;</code>

Result:

<code>../img</code>

This example uses a relative path ../img/pic.jpg ; dirname() returns the directory ../img .

Example 3:

<code>$path = "C:/xampp/htdocs/index.php";<br>$dir = dirname($path);<br>echo $dir;</code>

Result:

<code>C:/xampp/htdocs</code>

Here a Windows‑style path is provided, and dirname() returns C:/xampp/htdocs .

Example 4:

<code>$path = "/var/www/html";<br>$dir = dirname($path);<br>echo $dir;</code>

Result:

<code>/var/www</code>

When the given path itself is a directory, dirname() returns the parent directory, e.g., from /var/www/html it returns /var/www .

The dirname() function is a practical tool in PHP for extracting directory parts of paths, useful in dynamic path generation and file handling.

PHP learning recommendations:

Vue3+Laravel8+Uniapp Beginner to Practical Development Tutorial

Vue3+TP6+API Social E‑commerce System Development Course

Swoole From Beginner to Advanced Recommended Course

Workerman+TP6 Real‑time Chat System Limited‑time Offer

backend-developmentCode Examplefile pathdirnamedirectory extraction
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.