Current Location: Home> Latest Articles> How does PATHINFO_DIRNAME of pathinfo work?

How does PATHINFO_DIRNAME of pathinfo work?

gitbox 2025-06-03

In PHP, the pathinfo() function is a very practical function used to parse file paths. It can help us quickly obtain various components in the path, such as directory names, file names, extensions, etc. Among these components, PATHINFO_DIRNAME is often overlooked or misunderstood by beginners. Today, let’s take a deeper look at what PATHINFO_DIRNAME is and what its function is.

What is a pathinfo function?

The pathinfo() function is used to parse a file path and return the path composition information. Its basic syntax is:

 pathinfo(string $path, int $options = PATHINFO_ALL): array|string
  • $path : The file path to parse.

  • $options : Specifies the information part to be retrieved. It can be one of the following constants:

    • PATHINFO_DIRNAME : Return to the directory part in the path

    • PATHINFO_BASENAME : Returns the file name (including extension)

    • PATHINFO_EXTENSION : Returns the file extension

    • PATHINFO_FILENAME : Returns the file name (excluding extension)

    • If not specified, the associative array containing all the above information is returned by default.

What is PATHINFO_DIRNAME?

PATHINFO_DIRNAME is an option of the pathinfo() function, which means that only returns the directory part in the path. In other words, it tells you the directory path where the file is located, without including the file name itself.

For example:

 <?php
$path = "/var/www/html/index.php";
$dirname = pathinfo($path, PATHINFO_DIRNAME);
echo $dirname;
?>

Output:

 /var/www/html

Here PATHINFO_DIRNAME returns the directory where the file is located /var/www/html , instead of the file name index.php .

Functions and scenarios

The role of PATHINFO_DIRNAME is mainly reflected in the following aspects:

  1. Get the directory where the file is located <br> Using PATHINFO_DIRNAME is very convenient when you just want to know which directory the file is in and don't care about the file name.

  2. Directory Operation <br> When operating on file paths, such as moving files, copying files, or generating new file paths, you often need to get the directory part first.

  3. Prevent path errors <br> When stitching the path, first obtain the directory path and then append a new file name or subdirectory to avoid path stitching errors.

Combined with actual examples

Suppose you have an uploaded file path, you want to get the directory where the file is located, and generate a new file under that directory:

 <?php
$uploadPath = "/uploads/images/photo.jpg";

// Get the directory name
$dir = pathinfo($uploadPath, PATHINFO_DIRNAME);

// Generate new file path
$newFile = $dir . "/photo_thumb.jpg";

echo "New file path:" . $newFile;
?>

Output:

 New file path:/uploads/images/photo_thumb.jpg

Through PATHINFO_DIRNAME , we easily get the directory where the file is uploaded and generate a new file path under the same directory.

Things to note

  • When the path does not contain a directory part, PATHINFO_DIRNAME will return a point "." to indicate the current directory:

 <?php
$path = "file.txt";
echo pathinfo($path, PATHINFO_DIRNAME);
?>

Output:

 .
  • PATHINFO_DIRNAME only returns the directory path and does not contain file names or extensions.

  • In Windows systems, the path separator is backslash \ , and pathinfo will be processed automatically.

Summarize

PATHINFO_DIRNAME is an important option of the pathinfo() function, used to obtain the directory part in the path. It is very useful in file path processing and directory operations. It can help you easily obtain the directory where the file is located, making it easier to follow-up path stitching and file operations.

Mastering its usage will make you more comfortable processing of file paths in PHP.


 <?php
$examplePath = "https://gitbox.net/path/to/file.txt";
$dirName = pathinfo($examplePath, PATHINFO_DIRNAME);
echo $dirName; // Output:https://gitbox.net/path/to
?>