PHP pathinfo() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, pathinfo() splits a file path into its parts: the folder, the file name, the extension and the name without the extension.

It is the quick way to find the extension of an uploaded file or to build a new name from an old one. It only works with the text of the path, so the file does not have to exist.

Syntax

Call pathinfo() with the values you need. Start with this line.

PHP
$info = pathinfo('/var/www/photo.jpg');
CallWhat it does
pathinfo('/var/www/photo.jpg')Returns an array with dirname, basename, extension and filename.
pathinfo('photo.jpg', PATHINFO_EXTENSION)Returns only the string jpg.
pathinfo('/var/www/photo.jpg', PATHINFO_FILENAME)Returns only the name without the folder or extension, which is photo.

The flags are PATHINFO_DIRNAME, PATHINFO_BASENAME, PATHINFO_EXTENSION and PATHINFO_FILENAME. If you pass one of them, you get a string instead of an array. If the path has no extension, the extension key is missing from the array.

Library declarations
PHP
pathinfo(string $path, int $flags = PATHINFO_ALL): array|string

Parameters

ParameterDescription
pathRequired. The path to analyze.
flagsOptional. One of the PATHINFO_* constants to return only that part. The default is PATHINFO_ALL, which returns an array.

Return Value

Return ValueDescription
array|stringReturns an array of parts when no flag is given, or a string when one flag is given. The string is empty if that part does not exist.

PHP Examples

Example 1 — Split a Path Into Parts

Print every part of a file path.

PHP
<?php
$info = pathinfo('/var/www/html/photo.final.jpg');

print_r($info);

How It Works

  1. dirname is everything before the last slash.
  2. basename is the file name with its extension.
  3. extension is the text after the last dot, here jpg.
  4. filename is the basename without the extension, so photo. final stays together.

Example 2 — A File With No Extension

When there is no dot in the name, the extension key does not exist.

PHP
<?php
$info = pathinfo('/var/www/README');

echo "Extension: ", $info['extension'] ?? 'none', PHP_EOL;
echo "Name: ", $info['filename'], PHP_EOL;

How It Works

  1. README has no dot, so pathinfo() leaves out the extension key.
  2. The ?? operator provides the word none when the key is missing.
  3. The filename key still exists and holds README.
  4. Always use ?? or isset() when you read the extension from the array.

Example 3 — Get One Part with a Flag

Pass a flag to get a single string and build a new file name from it.

PHP
<?php
$path = '/uploads/My Photo.PNG';

$name = pathinfo($path, PATHINFO_FILENAME);
$ext  = strtolower(pathinfo($path, PATHINFO_EXTENSION));

echo $name . '-thumb.' . $ext, PHP_EOL;

How It Works

  1. PATHINFO_FILENAME returns My Photo, the name without the extension.
  2. PATHINFO_EXTENSION returns PNG and strtolower() changes it to png.
  3. The two parts are joined with -thumb. in the middle.
  4. The result is a new thumbnail name that keeps the original name.

Frequently Asked Questions

It breaks a file path into dirname, basename, extension and filename. It works on the text only, so the file does not need to exist.
Use pathinfo($file, PATHINFO_EXTENSION). It returns the text after the last dot, or an empty string if there is none.
The path has no dot in its last part, so pathinfo() does not create an extension key. Use the null coalescing operator to handle it.
The extension is gz and the filename is archive.tar. Only the last dot counts.
No. A user can rename any file. Use it with an allowed list, and also check the real file type with finfo or mime_content_type().

Next: pclose()

pclose() closes a process that was opened with popen(). It waits for the command to finish.

pclose() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful