pathinfo

Returns information about a file path

Syntax

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

Parameters

path

The path to be parsed.

flags

If present, specifies a specific element to be returned; one of PATHINFO_DIRNAME, PATHINFO_BASENAME, PATHINFO_EXTENSION or PATHINFO_FILENAME. If flags is not specified, returns all available elements.

Return

If the flags parameter is not passed, an associative array containing the following elements is returned: dirname, basename, extension (if any), and filename. If flags is present, returns a string containing the requested element.

Note: If the path has more than one extension, PATHINFO_EXTENSION returns only the last one and PATHINFO_FILENAME only strips the last one.

Note: If the path does not have an extension, no extension element will be returned.

Note: If the basename of the path starts with a dot, the following characters are interpreted as extension, and the filename is empty.

Examples

1

<?

$path = '/www/htdocs/inc/lib.inc.php';

$return = pathinfo($path);

print_r($return);

?>
Array
(
    [dirname] => /www/htdocs/inc
    [basename] => lib.inc.php
    [extension] => php
    [filename] => lib.inc
)

2

<?

$path = '/www/htdocs/inc/lib.inc.php';

$return = pathinfo($path);

echo $return['dirname'] . PHP_EOL;
echo $return['basename'] . PHP_EOL;
echo $return['extension'] . PHP_EOL;
echo $return['filename'];

?>
/www/htdocs/inc
lib.inc.php
php
lib.inc

3

<?

$path = '/www/htdocs/inc/lib.inc.php';
$flags = PATHINFO_DIRNAME;

$return = pathinfo($path, $flags);

echo $return;

?>
/www/htdocs/inc

4

<?

$path = '/www/htdocs/inc/lib.inc.php';
$flags = PATHINFO_BASENAME;

$return = pathinfo($path, $flags);

echo $return;

?>
lib.inc.php

5

<?

$path = '/www/htdocs/inc/lib.inc.php';
$flags = PATHINFO_EXTENSION;

$return = pathinfo($path, $flags);

echo $return;

?>
php

6

<?

$path = '/www/htdocs/inc/lib.inc.php';
$flags = PATHINFO_FILENAME;

$return = pathinfo($path, $flags);

echo $return;

?>
lib.inc

7

<?

$path = '/file.extension';

$return = pathinfo($path);

print_r($return);

?>
Array
(
    [dirname] => /
    [basename] => file.extension
    [extension] => extension
    [filename] => file
)

8

<?

$path = '/file.';

$return = pathinfo($path);

print_r($return);

?>
Array
(
    [dirname] => /
    [basename] => file.
    [extension] => 
    [filename] => file
)

9

<?

$path = '/file';

$return = pathinfo($path);

print_r($return);

?>
Array
(
    [dirname] => /
    [basename] => file
    [filename] => file
)

10

<?

$path = '/';

$return = pathinfo($path);

print_r($return);

?>
Array
(
    [dirname] => /
    [basename] => 
    [filename] => 
)

11

<?

$path = '';

$return = pathinfo($path);

print_r($return);

?>
Array
(
    [basename] => 
    [filename] => 
)

12

<?

$path = 'file.extension';

$return = pathinfo($path);

print_r($return);

?>
Array
(
    [dirname] => .
    [basename] => file.extension
    [extension] => extension
    [filename] => file
)

13

<?

$path = 'file.';

$return = pathinfo($path);

print_r($return);

?>
Array
(
    [dirname] => .
    [basename] => file.
    [extension] => 
    [filename] => file
)

14

<?

$path = 'file';

$return = pathinfo($path);

print_r($return);

?>
Array
(
    [dirname] => .
    [basename] => file
    [filename] => file
)

15

<?

$path = '.extension';

$return = pathinfo($path);

print_r($return);

?>
Array
(
    [dirname] => .
    [basename] => .extension
    [extension] => extension
    [filename] => 
)

16

<?

$path = '.';

$return = pathinfo($path);

print_r($return);

?>
Array
(
    [dirname] => .
    [basename] => .
    [extension] => 
    [filename] => 
)

17

<?

$path = '/.extension';

$return = pathinfo($path);

print_r($return);

?>
Array
(
    [dirname] => /
    [basename] => .extension
    [extension] => extension
    [filename] => 
)

18

<?

$path = '/.';

$return = pathinfo($path);

print_r($return);

?>
Array
(
    [dirname] => /
    [basename] => .
    [extension] => 
    [filename] => 
)

PHP

Home Menu