如果我有这样的代码:
$file = basename($filename);
Run Code Online (Sandbox Code Playgroud)
我如何获得文件扩展名$file?变量$file可以包含任何类型的文件,例如index.php或test.jpeg.
cle*_*tus 34
使用pathinfo()功能:
$path_parts = pathinfo('/www/htdocs/index.html');
echo $path_parts['dirname'], "\n";
echo $path_parts['basename'], "\n";
echo $path_parts['extension'], "\n";
echo $path_parts['filename'], "\n";
Run Code Online (Sandbox Code Playgroud)
或者干脆:
echo pathinfo($file, PATHINFO_EXTENSION);
Run Code Online (Sandbox Code Playgroud)
你当然可以寻找最后一个"." 在文件名中并获得一切(相对容易),但为什么重新发明轮子?