I want to share a tip for you to get the file extension in PHP easily. Before, I always used the old way to get the file extension, that is using substring and finding the last “.” (dot), and getting the extension. For those who are still doing this, I have a tip for you to get the extension more easily.
You can use pathinfo. With pathinfo you can get information about a file path. Here is an example of how to use it:
<?php
$fileinfo = pathinfo("/path/to/your/file.php");
echo $fileinfo['dirname']; // Get directory
echo $fileinfo['basename']; // Get file basename
echo $fileinfo['extension']; // Get file extension
echo $fileinfo['filename']; // Get file name
?>
That’s it! Thank you for visiting my blog and have a nice day…
