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] => 
)

basename

chgrp

chmod

chown

clearstatcache

copy

dirname

disk_free_space

disk_total_space

diskfreespace

fclose

feof

fflush

fgetc

fgetcsv

fgets

file

file_exists

file_get_contents

file_put_contents

fileatime

filectime

filegroup

fileinode

filemtime

fileowner

fileperms

filesize

filetype

flock

fnmatch

fopen

fpassthru

fputcsv

fputs

fread

fscanf

fseek

fstat

ftell

ftruncate

fwrite

glob

is_dir

is_executable

is_file

is_link

is_readable

is_uploaded_file

is_writable

is_writeable

lchgrp

lchown

link

linkinfo

lstat

mkdir

move_uploaded_file

pclose

popen

readfile

readlink

realpath

realpath_cache_get

realpath_cache_size

rename

rewind

rmdir

set_file_buffer

stat

symlink

tempnam

tmpfile

touch

umask

unlink

PHP

Home Menu