How do I get a file extension in PHP?

前端 未结 28 2232
一向
一向 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:15

    IMO, this is the best way if you have filenames like name.name.name.ext (ugly, but it sometimes happens):

    $ext     = explode('.', $filename); // Explode the string
    $ext_len = count($ext) - 1; // Count the array -1 (because count() starts from 1)
    $my_ext  = $ext[$ext_len]; // Get the last entry of the array
    
    echo $my_ext;
    

提交回复
热议问题