一、对于普通的文件路径,可以使用pathinfo:
$file_parts = pathinfo($filename);
switch($file_parts['extension']){
case "jpg":
break;
case "exe":
break;
case "": // Handle file extension for files ending in '.'
case NULL: // Handle no file extension
break;}
二、或者借助explode方法来实现:
$name = "song.mp3";
$parts = explode('.', $name);
$extension = array_pop($parts);
if( $extension == 'mp3'){
echo 'This is a mp3 file.';}