PHP pathinfo()Functions

PHP Filesystem 参考手册Complete PHP Filesystem Reference Manual


Definition and Usage

The pathinfo() function returns information about a file path as an array.

The returned array elements are as follows:

  • [dirname]: Directory path
  • [basename]: File name
  • [extension]: File extension
  • [filename]: File name without extension

Syntax

pathinfo(path,options)

Parameter Description
path Required. Specifies the path to be checked.
options Optional. Specifies the array elements to return. Default is all.

Possible values:

  • PATHINFO_DIRNAME - returns only dirname
  • PATHINFO_BASENAME - returns only basename
  • PATHINFO_EXTENSION - returns only extension
  • PATHINFO_FILENAME - returns only filename


Tips and Notes

Note:If not all elements are requested, the pathinfo() function returns a string.


Examples

Example 1

<?php print_r(pathinfo("/testweb/test.txt")); ?>

The above code will output:

Array
(
    [dirname] => /testweb
    [basename] => test.txt
    [extension] => txt
    [filename] => test
)

Example 2

<?php print_r(pathinfo("/testweb/test.txt",PATHINFO_BASENAME)); ?>

The above code will output:

test.txt


PHP Filesystem 参考手册Complete PHP Filesystem Reference Manual

Other Extensions