Skip to content
PHP Logo

How can pathinfo() function be utilized in PHP to extract file extension?

To extract the file extension using the pathinfo() function in PHP, you can pass the PATHINFO_EXTENSION constant as the second parameter to the function. This will return the extension of the file specified in the path.

php
// Example code to extract file extension using pathinfo() function
$file = 'example.txt';
$extension = pathinfo($file, PATHINFO_EXTENSION);

echo $extension; // Output: txt