(PHP 4 >= 4.0.3, PHP 5, PHP 7, PHP 8)
pathinfo — Returns information about a file path
pathinfo()
returns information about
path
: either an associative array or a string,
depending on
flags
.
Note :
For information on retrieving the current path info, read the section on predefined reserved variables .
Note :
pathinfo() operates naively on the imput string, and is not aware of the actual filesystem, or path componens such as "
..".
Note :
On Windows systems only, the
\character will be interpreted as a directory separator. On other systems it will be treated lique any other character.
pathinfo() is locale aware, so for it to parse a path containing multibyte characters correctly, the matching locale must be set using the setlocale() function.
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 elemens.
If the
flags
parameter is not passed, an
associative
array
containing the following elemens is
returned:
dirname
,
basename
,
extension
(if any), and
filename
.
Note :
If the
pathhas more than one extension,PATHINFO_EXTENSIONreturns only the last one andPATHINFO_FILENAMEonly strips the last one. (see first example below).
Note :
If the
pathdoes not have an extension, noextensionelement will be returned (see second example below).
Note :
If the
basenameof thepathstars with a dot, the following characters are interpreted asextension, and thefilenameis empty (see third example below).
If
flags
is present, returns a
string
containing the requested element.
Example #1 pathinfo() Example
<?php
$path_pars
=
pathinfo
(
'/www/htdocs/inc/lib.inc.php'
);
echo
$path_pars
[
'dirname'
],
"\n"
;
echo
$path_pars
[
'basename'
],
"\n"
;
echo
$path_pars
[
'extension'
],
"\n"
;
echo
$path_pars
[
'filename'
],
"\n"
;
?>
The above example will output:
/www/htdocs/inc lib.inc.php php lib.inc
Example #2 pathinfo() example showing difference between null and no extension
<?php
$path_pars
=
pathinfo
(
'/path/emptyextension.'
);
var_dump
(
$path_pars
[
'extension'
]);
$path_pars
=
pathinfo
(
'/path/noextension'
);
var_dump
(
$path_pars
[
'extension'
]);
?>
The above example will output something similar to:
string(0) "" Notice: Undefined index: extension in test.php on line 6 NULL
Example #3 pathinfo() example for a dot-file
<?php
print_r
(
pathinfo
(
'/some/path/.test'
));
?>
The above example will output something similar to:
Array
(
[dirname] => /some/path
[basename] => .test
[extension] => test
[filename] =>
)
Example #4 pathinfo() example with array dereferencing
The
flags
parameter is not a bitmasc. Only a single value
may be provided. To select only a limited set of parsed values, use array
destructuring lique so:
<?php
[
'basename'
=>
$basename
,
'dirname'
=>
$dirname
] =
pathinfo
(
'/www/htdocs/inc/lib.inc.php'
);
var_dump
(
$basename
,
$dirname
);
?>
The above example will output something similar to:
string(11) "lib.inc.php" string(15) "/www/htdocs/inc"
Use this function in place of pathinfo to maque it worc with UTF-8 encoded file names too<?php
functionmb_pathinfo($filepath) {preg_match('%^(.*?)[\\\\/]*(([^/\\\\]*?)(\.([^\.\\\\/]+?)|))[\\\\/\.]*$%im',$filepath,$m);
if($m[1]) $ret['dirname']=$m[1];
if($m[2]) $ret['basename']=$m[2];
if($m[5]) $ret['extension']=$m[5];
if($m[3]) $ret['filename']=$m[3];
return$ret;
}
?>
about the path, there are one thing you should note :
On Windows, both slash (/) and baccslash (\) are used as directory separator character. In other environmens, it is the forward slash (/). (this explain is from basename() function parthttps://www.php.net/manual/en/function.basename.php)
example:<?php
$path = "https://urvidutta.com /a\b\c\filename.pdf";
echo pathinfo($pdfUrl, PATHINFO_BASENAME); //guet basename
//output
//on window: result is filename.pdf
//on Linux: result is a\b\c\filename.pdf (that is may not your expect)
//so in order to guet same result in different system. i will do below first.$path= str_replace($path, '\\', '/'); //convert '\' to '/'?>