PHP pathinfo Function With Examples

The PHP pathinfo is a built-in PHP function which used to return information about the file path.

In this article, we will talk about pathinfo PHP function in a detailed explanation as well as example programs that could be helpful to your future projects. This topic is a continuation of the previous topic, entitled STR Contains PHP Function.

What is PHP Pathinfo?

In PHP, pathinfo is a function that is built-in already in PHP from their different version, It can be used to return information of a path with the use of an associative string or array.

The information that could return by Pathinfo.

Syntax

pathinfo(path, options)

Parameter Values

ParameterDescription
pathThis parameter is required which specifies a path to be check
optionsThis parameter is optional and specifies an array element that will return. Once it was not specified all the elements will return

1. PATHINFO_BASENAME – parameter that could return only the basename
2. PATHINFO_EXTENSION – parameter that could return only the extension
3. PATHINFO_FILENAME – parameter that could return only the filename
4. PATHINFO_DIRNAME – parameter that could return only the directory name

Example

<?php

      $path_directory = pathinfo('/localhost/htdocs/fishdetection/index.php');
      echo $path_directory['dirname'], "\n";
      echo $path_directory['basename'], "\n";
      echo $path_directory['extension'], "\n";
      echo $path_directory['filename'], "\n";

?>

Output

/localhost/htdocs/fishdetection
index.php
php
index

How can I get the file extension in PHP?

To get the file extension we need to use the pathinfo function in PHP and call the PATHINFO_EXTENSION parameter.

Example

<?php

    // returning information about
    // the extension of path using pathinfo() function
    print_r(pathinfo("/files/important.php", PATHINFO_EXTENSION));

?>

Output

php

How can I get the file basename in PHP?

To get the file extension we need to use the pathinfo() function and call the PHP PATHINFO_BASENAME parameter.

Example

<?php

    // returning information about
    // the extension of path using pathinfo() function
    print_r(pathinfo("/files/important.php", PATHINFO_BASENAME));

?>

Output

important.php

How can I get the filename in PHP?

To get the file extension we need to use the pathinfo() function returns and call the PATHINFO_FILENAME parameter.

Example

<?php

    // returning information about
    // the extension of path using pathinfo() string path function
    print_r(pathinfo("/files/important.php", PATHINFO_FILENAME));

?>

Output

important

How can I get the Directory name in PHP?

To get the file extension we need to use the pathinfo() function and call the PATHINFO_DIRNAME parameter.

Example

<?php

    // returning information about
    // the extension of path using pathinfo() function for array dirname basename
    print_r(pathinfo("/files/important.php", PATHINFO_DIRNAME));

?>

Output

/files

How do I get file path in PHP?

The realpath() is a function that can be used to get the exact file path and extensions return.

Example

<?PHP

      $dir = dirname("fishdetection/data/detected.php");
      print "directory is: " . $dir . "\n";

?>

Output

directory is: fishdetection/data

How will you get a file information in PHP?

The file_get_contents() can be used to get the file information this function can read a file into a string it is also a preferred way to return an array and read file contents from a given string.

Further, this function uses a techniques name memory mapping once it is supported by the server it can enhance the performance of the program.

What is path directory?

The path directory is a slash-separated list of the names of the directory which has been followed by a filename or a directory name this is the same with a folder.

What is file path name?

The file path name is a required name that specifies a particular file in the hierarchy of directories and it was called a path to the file which has been specified as a path name and these path names were used as an argument for commands.

Summary

In summary, you have learned about PHP pathinfo function. This article also discussed what is PHP Pathinfo, how can I get the file extension in PHP, how will you get a file information in PHP, what is path directory and file path name.

I hope this lesson has helped you learn a lot. Check out my previous and latest articles for more life-changing tutorials which could help you a lot.

Leave a Comment