What is the pathinfo() function in PHP?

Overview

The pathinfo function in PHP is used to acquire more details about the path of a file. This means we can know about its directory name, base name, etc. The general syntax for the pathinfo file is as follows:

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

Parameters

There are two parameters for the pathinfo function.

  1. path: This parameter is compulsory and is the path to the file you wish to acquire more details for.

  2. options: This is optional but it is used when you wish to know a particular detail about the file. You can enter the following in the options parameter:

    • PATHINFO_DIRNAME: This will only return the directory name for the file.
    • PATHINFO_BASENAME: This will only return the base name for the file.
    • PATHINFO_EXTENSION: This will only return the extension for the file.
    • PATHINFO_FILENAME: This will only return the file name for the file.

Return value

The pathinfo function returns the details of the file according to the specified options.

In case options is not specified, an array of all details regarding the file is returned. Moreover, if there is a failure to access any details, pathinfo returns FALSE.

Example

The following example will help you understand pathinfo better. As shown below, the pathinfo function returns information regarding a file’s directory name, base name, extension, and file name in the form of an array.

<?php
print_r(pathinfo("/file/main.txt"));
?>

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved