How do I get a file extension in PHP?

前端 未结 28 2318
一向
一向 2020-11-21 22:45

This is a question you can read everywhere on the web with various answers:

$ext = end(explode(\'.\', $filename));
$ext = substr(strrchr($filename, \'.\'), 1         


        
28条回答
  •  遥遥无期
    2020-11-21 23:24

    You can try also this (it works on PHP 5.* and 7):

    $info = new SplFileInfo('test.zip');
    echo $info->getExtension(); // ----- Output -----> zip
    

    Tip: it returns an empty string if the file doesn't have an extension

提交回复
热议问题