pathinfo
(PHP 4 >= 4.0.3, PHP 5, PHP 7)
pathinfo — Returns information about a file path
Description
$path
[, int $options
= PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION | PATHINFO_FILENAME
] ) : mixed
pathinfo() returns information about
path
: either an associative array or a string,
depending on options
.
Note:
For information on retrieving the current path info, read the section on predefined reserved variables.
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.
Parameters
-
path
-
The path to be parsed.
-
options
-
If present, specifies a specific element to be returned; one of
PATHINFO_DIRNAME
,PATHINFO_BASENAME
,PATHINFO_EXTENSION
orPATHINFO_FILENAME
.If
options
is not specified, returns all available elements.
Return Values
If the options
parameter is not passed, an
associative array containing the following elements is
returned:
dirname, basename,
extension (if any), and filename.
Note:
If the
path
has more than one extension,PATHINFO_EXTENSION
returns only the last one andPATHINFO_FILENAME
only strips the last one. (see first example below).
Note:
If the
path
does not have an extension, no extension element will be returned (see second example below).
Note:
If the basename of the
path
starts with a dot, the following characters are interpreted as extension, and the filename is empty (see third example below).
If options
is present, returns a
string containing the requested element.
Examples
Example #1 pathinfo() Example
<?php
$path_parts = pathinfo('/www/htdocs/inc/lib.inc.php');
echo $path_parts['dirname'], "\n";
echo $path_parts['basename'], "\n";
echo $path_parts['extension'], "\n";
echo $path_parts['filename'], "\n"; // since PHP 5.2.0
?>
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_parts = pathinfo('/path/emptyextension.');
var_dump($path_parts['extension']);
$path_parts = pathinfo('/path/noextension');
var_dump($path_parts['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] => )
See Also
- dirname() - Returns a parent directory's path
- basename() - Returns trailing name component of path
- parse_url() - Parse a URL and return its components
- realpath() - Returns canonicalized absolute pathname
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-pathinfo.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.