PHP | pathinfo( ) Function

The pathinfo() is an inbuilt function which is used to return information about a path using an associative array or a string.
The returned array or string contains the following information:

  • Directory name
  • Basename
  • Extension


The path and options are sent as a parameters to the pathinfo() function and it returns an associative array containing the following elements directory name, basename, extension if the options parameter is not passed.

Syntax:

pathinfo(path, options)

Parameters Used:
The pathinfo() function in PHP accepts two parameters.

  1. path : It is a mandatory parameter which specifies the path of the file.
  2. options : It is an optional parameter which can used to restrict the elements returned by the pathinfo() function. By deafult it returns all the possible values which are directory name, basename, extension.
    Possible values can be restricted using :

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

Return Value:
It returns an associative array containing the following elements directory name, basename, extension if the options parameter is not passed.

Errors And Exceptions:

  1. PATHINFO_EXTENSION returns only the last extension, if the path has more than one extension.
  2. No extension element is returned, if the path does not have an extension.
  3. If the basename of the path starts with a dot, the following characters are interpreted as extension, and the filename is empty.

Examples:

Input : print_r(pathinfo("/documents/gfg.txt"));
Output : Array
         (
          [dirname] => /documents
          [basename] => gfg.txt
          [extension] => txt
         )

Input : print_r(pathinfo("/documents/gfg.txt", PATHINFO_DIRNAME));
Output : /documents

Input : print_r(pathinfo("/documents/gfg.txt", PATHINFO_EXTENSION));
Output : txt

Input : print_r(pathinfo("/documents/gfg.txt", PATHINFO_BASENAME));
Output : gfg.txt

Below programs illustrate the pathinfo() function.

Suppose there is a file named “gfg.txt”

Program 1

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// returning information about 
// the path using pathinfo() function
print_r(pathinfo("/documents/gfg.txt"));
?>

chevron_right


Output:

 Array
         (
          [dirname] => /documents
          [basename] => gfg.txt
          [extension] => txt
         )

Program 2

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
  // returning information about 
  // the directoryname path using pathinfo() function
  print_r(pathinfo("/documents/gfg.txt", PATHINFO_DIRNAME));
?>

chevron_right


Output:

 /documents 

Program 3

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
  
  // returning information about 
  // the extension of path using pathinfo() function
  print_r(pathinfo("/documents/gfg.txt", PATHINFO_EXTENSION));
?>

chevron_right


Output:

 txt 

Program 4

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
  // returning information about 
  // the basename of path using pathinfo() function
  print_r(pathinfo("/documents/gfg.txt", PATHINFO_BASENAME));
?>

chevron_right


Output:

 gfg.txt 

Reference:
http://php.net/manual/en/function.pathinfo.php



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.