Filesystem 函数
PHP Manual

pathinfo

(PHP 4 >= 4.0.3, PHP 5)

pathinfo返回文件路径的信息

说明

mixed pathinfo ( string $path [, int $options ] )

pathinfo() 返回一个关联数组包含有 path 的信息。包括以下的数组单元:dirnamebasenameextension

可以通过参数 options 指定要返回哪些单元。它们包括:PATHINFO_DIRNAMEPATHINFO_BASENAMEPATHINFO_EXTENSION。默认是返回全部的单元。如果不是要求取得所有单元,则本函数返回字符串。

Example #1 pathinfo() 例子

<?php
$path_parts 
pathinfo("/www/htdocs/index.html");
echo 
$path_parts["dirname"] . "\n";
echo 
$path_parts["basename"] . "\n";
echo 
$path_parts["extension"] . "\n";
?>

以上例程会输出:

/www/htdocs
index.html
html

Note:

有关取得当前路径信息的说明,请阅读预定义变量一节。

参见 dirname()basename()parse_url()realpath()

参数

path

The path to be parsed.

options

If present, specifies a specific element to be returned; one of PATHINFO_DIRNAME, PATHINFO_BASENAME, PATHINFO_EXTENSION or PATHINFO_FILENAME.

If options is not specified, returns all available elements.

返回值

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 does not have an extension, no extension element will be returned (see second example below).

If options is present, returns a string containing the requested element.

更新日志

版本 说明
5.2.0 The PATHINFO_FILENAME constant was added.

范例

Example #2 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
?>

以上例程会输出:

/www/htdocs/inc
lib.inc.php
php
lib.inc

Example #3 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']);
?>

以上例程的输出类似于:

string(0) ""

Notice: Undefined index: extension in test.php on line 6
NULL

注释

Note:

For information on retrieving the current path info, read the section on predefined reserved variables.

Note:

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.

参见


Filesystem 函数
PHP Manual