pathinfo() function in PHP

PHPProgrammingServer Side Programming

The pathinfo() function returns information about a file path in an array. The pathinfo() function returns an associative array with the following elements −

  • directory name − returns directory name

  • basename − returns basename

  • extension − returns extension

Syntax

pathinfo(path,options)

Parameters

  • path − The path to be checked.

  • options − Specify which of the elements to return

    • PATHINFO_DIRNAME - return only dirname
    • PATHINFO_BASENAME - return only basename
    • PATHINFO_EXTENSION - return only extension

Return

The pathinfo() function returns an associative array with the following elements.

  • directory name − returns directory name

  • basename − returns basename

  • extension − returns extension

The following is an example showing all the information since we haven’t set the second parameter.

Example

 Live Demo

<?php
   print_r(pathinfo("/images/architecture.png"));
?>

Output

Array
(
   [dirname] => /images
   [basename] => architecture.png
   [extension] => png
)

Let us see how to get only the directory name.

Example

 Live Demo

<?php
   print_r(pathinfo("/images/architecture.png",PATHINFO_DIRNAME));
?>

Output

/images

Let us see how to get only the basename.

Example

 Live Demo

<?php
   print_r(pathinfo("/images/architecture.png",PATHINFO_BASENAME));
?>

Output

architecture.png

Let us see how to get only the extension.

Example

 Live Demo

<?php
   print_r(pathinfo("/images/architecture.png",PATHINFO_EXTENSION));
?>

Output

png
raja
Published on 04-Oct-2018 08:34:33
Advertisements