use pathinfo function to get metadata(info about the file) of file
$file='display.txt';
$fileinfo=pathinfo($file);
if($fileinfo['extension']=='txt'){
echo file_get_contents($file);
}
or you can use
$file='display.txt';
$fileext=pathinfo($file,PATHINFO_EXTENSION );
if($fileext=='txt'){
echo file_get_contents($file);
}
function pathinfo give you array contain with directory,basename(filename with extension),extension and filename with out exension
$file='www/display.txt';
$fileinfo=pathinfo($file);
print_r($fileinfo);
give you output
Array ( [dirname] => www [basename] => display.txt [extension] => txt [filename] => display )
function pathinfo should have first argument filename and second will be option of output . you can pass second argument one of PATHINFO_DIRNAME, PATHINFO_BASENAME, PATHINFO_EXTENSION or PATHINFO_FILENAME.
if you pass PATHINFO_EXTENSION second argument in pathinfo function you will get extension as string not array
so
$file='www/display.txt';
$fileinfo=pathinfo($file,PATHINFO_EXTENSION );
echo($fileinfo);
give you output
txt