Latest web development tutorials

PHP pathinfo () function

PHP Filesystem Reference Manual Complete PHP Filesystem Reference Manual

Definition and Usage

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

The array element returned are as follows:

  • [Dirname]
  • [Basename]
  • [Extension]

grammar

pathinfo(path,options)

参数 描述
path 必需。规定要检查的路径。
options 可选。规定要返回的数组元素。默认是 all。

可能的值:

  • PATHINFO_DIRNAME - 只返回 dirname
  • PATHINFO_BASENAME - 只返回 basename
  • PATHINFO_EXTENSION - 只返回 extension


Tips and Notes

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


Example 1

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

The code above will output:

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


Example 2

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

The code above will output:

test.txt


PHP Filesystem Reference Manual Complete PHP Filesystem Reference Manual