Change language

PHP Pathinfo () function

Pathinfo() - it is a built-in function that is used to return path information using an associative array or string. 
The returned array or string contains the following information:
  • Directory name
  • Basename
  • extension
The path and parameters are sent as parameters to the pathinfo() function, and it returns an associative array containing the directory name, base name, extension of the following elements, if parameter parameter not passed.Syntax:
pathinfo (path, options)
Parameters used:
The pathinfo() function in PHP takes two parameters.
  • path:is a required parameter and specifies the path to the file .
  • options:is an optional parameter that can be used to limit the items returned by pathinfo(). By default, it returns all possible values: directory name, base name, extension. 
    Possible values ​​can be limited with:
    • PATHINFO_DIRNAME - return only dirname
    • PATHINFO_BASENAME - return base name only
    • PATHINFO_EXTENSION - return only extension
  • Return value:
    It returns an associative array containing the following item catalog name, base name, extension if options is not passed.Errors and Exceptions:
  • PATHINFO_EXTENSION only returns the last extension if the path has more than one extension.
  • No extension element is returned if the path does not have an extension.
  • If the base path name begins with a period, the following characters are interpreted as an 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 
    The programs below illustrate the pathinfo() function.Suppose there is a file named "gfg.txt"Program 1
    // return information about
    // path using the pathinfo() function print_r ( pathinfo ( "/ documents / gfg.txt" )); ?>
    Output:
    Array ([dirname] = > / documents [basename] = > gfg.txt [extension] = > txt) 
    Program 2 // return information about // path to name directory using the pathinfo() function print_r ( pathinfo ( " / documents / gfg.txt " , PATHINFO_DIRNAME)); ?> Output:
    / documents 
    Program 3 // return information o // expanding the path using the pathinfo() function print_r ( pathinfo ( " / documents / gfg.txt " , PATHINFO_EXTENSION)); ?> Output:
    txt 
    Program 4 // return information about // base pathname using pathinfo() print_r ( pathinfo ( "/ documents / gfg .txt " , PATHINFO_BASENAME)); ?> Output:
    gfg.txt 
    Link:
    http : //php.net/manual/en/function.pathinfo.php

    Shop

    Gifts for programmers

    Best laptop for Excel

    $
    Gifts for programmers

    Best laptop for Solidworks

    $399+
    Gifts for programmers

    Best laptop for Roblox

    $399+
    Gifts for programmers

    Best laptop for development

    $499+
    Gifts for programmers

    Best laptop for Cricut Maker

    $299+
    Gifts for programmers

    Best laptop for hacking

    $890
    Gifts for programmers

    Best laptop for Machine Learning

    $699+
    Gifts for programmers

    Raspberry Pi robot kit

    $150

    Latest questions

    PythonStackOverflow

    Common xlabel/ylabel for matplotlib subplots

    1947 answers

    PythonStackOverflow

    Check if one list is a subset of another in Python

    1173 answers

    PythonStackOverflow

    How to specify multiple return types using type-hints

    1002 answers

    PythonStackOverflow

    Printing words vertically in Python

    909 answers

    PythonStackOverflow

    Python Extract words from a given string

    798 answers

    PythonStackOverflow

    Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

    606 answers

    PythonStackOverflow

    Python os.path.join () method

    384 answers

    PythonStackOverflow

    Flake8: Ignore specific warning for entire file

    360 answers


    Wiki

    Python | How to copy data from one Excel sheet to another

    Common xlabel/ylabel for matplotlib subplots

    Check if one list is a subset of another in Python

    How to specify multiple return types using type-hints

    Printing words vertically in Python

    Python Extract words from a given string

    Cyclic redundancy check in Python

    Finding mean, median, mode in Python without libraries

    Python add suffix / add prefix to strings in a list

    Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

    Python - Move item to the end of the list

    Python - Print list vertically