Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Files and I/O

PHP pathinfo(), basename() and dirname() functions

Reference for PHP pathinfo(), basename(), dirname() and realpath(): split a path into directory, file name, extension and base name, with edge cases explained.

pathinfo() splits a path string into its parts: directory, file name, extension and file name without extension. basename() returns the last component and dirname() everything before it. These are pure string functions; they do not check that the path exists.

They are the right tools for building new file names, checking extensions and extracting the safe part of an upload name. realpath() is the related function that does touch the disk: it resolves .. and symlinks into an absolute path.

PHP
$parts = pathinfo("/var/www/photo.final.jpg");
echo $parts["extension"];   // jpg

Try it

PHP Splitting a path

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Practical helpers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
pathinfo(string $path, int $flags = PATHINFO_ALL): array|string
basename(string $path, string $suffix = ""): string
dirname(string $path, int $levels = 1): string
realpath(string $path): string|false
Name Description
$path The path string to analyze. It need not exist.
$flags One of PATHINFO_DIRNAME, PATHINFO_BASENAME, PATHINFO_EXTENSION, PATHINFO_FILENAME. Omit for an array of all parts.
$suffix (basename) A suffix to strip from the result.
$levels (dirname) How many directory levels to go up.

Return value: pathinfo() returns an array with dirname, basename, extension (only if there is one) and filename, or a string if a single flag is given (empty string when the part is missing). realpath() returns the canonical absolute path or false if it does not exist.

Usage notes

  • extension can be missing. In the array form the key is absent for names without a dot, so use $p["extension"] ?? "".
  • Only the last dot counts. archive.tar.gz has the extension gz and the filename archive.tar.
  • A leading dot is an extension: .htaccess has filename empty and extension htaccess.
  • dirname("file.txt") is ".", the current directory, and dirname("/") is "/".
  • Trailing slashes are ignored by basename(): basename("/a/b/") is b.
  • Locale and encoding. These functions are locale-sensitive for multibyte names on some systems; for UTF-8 file names, test with your data.
  • Security: basename() removes directory parts such as ../, which helps with uploaded names, but never trust an extension to prove the content type. Compare extensions in lowercase against a whitelist.
  • Both slash styles are understood on Windows; on Linux a backslash is an ordinary character.
  • realpath() touches the disk, resolves symlinks and .., and returns false for missing paths.
  • Use __DIR__ with dirname() to find project folders: dirname(__DIR__) is the parent of the current script's folder.