W3docs

pathinfo()

The pathinfo() function is a built-in PHP function that returns information about a file path. It takes a file path as its argument and returns an associative

What is the pathinfo() Function?

The pathinfo() function is a built-in PHP function that splits a file path into its parts — the directory, the file name, and the extension — and returns them as an associative array. It is the go-to helper whenever you need to answer questions like "what folder is this file in?" or "what's the extension of an uploaded file?" without writing your own string parsing.

A key thing to understand up front: pathinfo() works purely on the string you give it. It never touches the filesystem, so the path does not have to point at a file that actually exists. This makes it fast and safe to call on user-supplied values (for example, the original name of an uploaded file).

This page covers the syntax, the array pathinfo() returns, how to ask for a single component, and the edge cases that trip people up (no extension, dotfiles, multi-part extensions).

Syntax

pathinfo(string $path, int $flags = PATHINFO_ALL): array|string
  • $path — the file path to inspect. It can be absolute or relative and need not exist.
  • $flags — optional. When omitted (or set to PATHINFO_ALL), the function returns an associative array. When set to a single flag, it returns just that component as a string.

The array pathinfo() returns

By default pathinfo() returns an associative array with up to four keys:

KeyMeaningExample for /var/www/html/index.php
dirnameThe directory part/var/www/html
basenameThe full file name with extensionindex.php
extensionThe extension (without the dot)php
filenameThe file name without extensionindex
php— editable, runs on the server

Here we pass the file path and store the resulting array in $info, then read each component by its key.

Important: the extension key only exists when the path actually contains a .. If the path has no extension, that key is simply absent from the array, so accessing $info['extension'] directly would raise an "Undefined array key" warning. Guard it with $info['extension'] ?? '' when in doubt.

Returning a single component

If you only need one piece, pass a flag as the second argument. The possible flags are PATHINFO_DIRNAME, PATHINFO_BASENAME, PATHINFO_EXTENSION, and PATHINFO_FILENAME. With a single flag the function returns a string instead of an array:

php— editable, runs on the server

This is the cleanest way to grab just the extension — for example, to check that an upload is an allowed type.

Edge cases to watch for

pathinfo() is forgiving, but a few results surprise people:

<?php

// 1. No extension: the 'extension' key is missing entirely.
print_r(pathinfo('/etc/hosts'));
// Array ( [dirname] => /etc [basename] => hosts [filename] => hosts )

// 2. Multi-part extension: only the LAST part counts as the extension.
print_r(pathinfo('archive.tar.gz'));
// Array ( [dirname] => . [basename] => archive.tar.gz
//         [extension] => gz [filename] => archive.tar )

// 3. Dotfile: the leading dot makes the whole name the "extension".
print_r(pathinfo('/home/user/.bashrc'));
// Array ( [dirname] => /home/user [basename] => .bashrc
//         [extension] => bashrc [filename] =>  )

Notes:

  • A path with no directory (case 2) reports dirname as . (the current directory).
  • For .tar.gz-style names, pathinfo() treats only gz as the extension — there is no built-in way to get tar.gz in one call.
  • A dotfile like .bashrc is read as "a file named nothing with extension bashrc," which is rarely what you want. Handle such cases yourself if your code allows them.

When to use pathinfo()

Reach for pathinfo() when you need more than one part of a path at once, or when you want a readable, named array instead of remembering which dedicated function does what:

  • Use pathinfo($p) to get the directory, name, and extension together.
  • Use basename() if you only want the file name — it also lets you strip a known suffix.
  • Use dirname() if you only want the directory, and it handles multiple levels via its $levels argument.
  • Use realpath() when you need to resolve a path against the actual filesystem (symlinks, .., etc.) — unlike pathinfo(), it does hit the disk and returns false for missing files.

For a broader overview of reading and writing files, see PHP File Handling.

Conclusion

pathinfo() is the quickest way to break a file path into its directory, base name, extension, and bare file name. Remember that it only parses the string (it never checks whether the file exists), that the extension key is absent when there is no extension, and that only the final extension is reported for names like archive.tar.gz. For single-component needs, the dedicated basename() and dirname() functions are often clearer.

Practice

Practice
What is the purpose of the 'pathinfo()' function in PHP?
What is the purpose of the 'pathinfo()' function in PHP?
Was this page helpful?