find the extension of a file in php

To find the extension of a file in PHP, you can use a combination of the pathinfo() function and string manipulation.

Here is an example code block that demonstrates how to find the extension of a file:

main.php
$filename = 'example.jpg';
$extension = pathinfo($filename, PATHINFO_EXTENSION);
echo $extension; // Output: jpg
113 chars
4 lines

In this example:

  • The $filename variable holds the name of the file for which we want to find the extension.
  • The pathinfo() function returns an associative array with information about the file path, including the extension. We use the PATHINFO_EXTENSION constant to specify that we want to retrieve only the extension from the array.
  • The $extension variable holds the extension of the file.
  • Finally, we use an echo statement to print the extension to the screen. In this case, it will output "jpg".

gistlibby LogSnag