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.
$parts = pathinfo("/var/www/photo.final.jpg"); echo $parts["extension"]; // jpg
Try it
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.
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
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
extensioncan 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.gzhas the extensiongzand the filenamearchive.tar. - A leading dot is an extension:
.htaccesshasfilenameempty andextensionhtaccess. dirname("file.txt")is".", the current directory, anddirname("/")is"/".- Trailing slashes are ignored by
basename():basename("/a/b/")isb. - 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 returnsfalsefor missing paths.- Use
__DIR__withdirname()to find project folders:dirname(__DIR__)is the parent of the current script's folder.