Skip to content

Instantly share code, notes, and snippets.

@IftiMahmud
Created January 5, 2015 06:40
Show Gist options
  • Save IftiMahmud/d39dbfa0e120a96c5b7d to your computer and use it in GitHub Desktop.
Save IftiMahmud/d39dbfa0e120a96c5b7d to your computer and use it in GitHub Desktop.
File Extension Finder in PHP
<?php
//Elegant Way to Find File Extension in PHP
// WAY-1:
$file_name = "ding.txt";
echo substr(strrchr($file_name,'.'),1).'<br/>'; //Shows ext. eg; txt
echo substr(strrchr($file_name,'.'),0).'<br/>'; //Shows ext followed by a dot (.); eg; .txt
//WAY-2:
$file_name = pathinfo('/www/htdocs/inc/lib.inc.php');
echo $file_name['dirname'].'<br/>';
echo $file_name['basename'].'<br/>';
echo $file_name['extension'].'<br/>';
echo $file_name['filename'].'<br/>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment