pathinfo
Splits a path into parts: the folder, the name and the extension.
Syntax
pathinfo(string $path, int $flags = PATHINFO_ALL): array|stringParameters
| Parameter | Type | Description |
|---|---|---|
| $path | string | The path. |
| $flags | int | Optional. Which part to return: `PATHINFO_EXTENSION`, `PATHINFO_FILENAME` and others. |
Return value
array|stringAn array of the path's parts, or a single part as a string.
Examples
Every part of a path:
<?php
echo json_encode(pathinfo('/var/www/photo.final.jpg'), JSON_UNESCAPED_SLASHES);Output:
{"dirname":"/var/www","basename":"photo.final.jpg","extension":"jpg","filename":"photo.final"}The extension only:
<?php
echo strtolower(pathinfo('PHOTO.JPG', PATHINFO_EXTENSION));Output:
jpgNotes
The extension is whatever follows the last dot. It says nothing about the real content of a file: for uploads check the type by content.
See also
- basename — Returns the last part of a path — the file name.
- file_exists — Checks whether a file or folder exists.
Related recipes
In the tutorial
- Folders and paths — Working with folders in PHP — creating them, listing files, searching by a mask, parts of a path and file information.