You can use substr to get the portion of the text before the last .
$basefile = substr($file,0,strrpos($file,'.'));
If you add that to the while loop in the above function you can use $basefile to refer to the filename without the extension.