How do I get a file extension in PHP?

前端 未结 28 2246
一向
一向 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条回答
  •  -上瘾入骨i
    2020-11-21 23:09

    A quick fix would be something like this.

    // Exploding the file based on the . operator
    $file_ext = explode('.', $filename);
    
    // Count taken (if more than one . exist; files like abc.fff.2013.pdf
    $file_ext_count = count($file_ext);
    
    // Minus 1 to make the offset correct
    $cnt = $file_ext_count - 1;
    
    // The variable will have a value pdf as per the sample file name mentioned above.
    $file_extension = $file_ext[$cnt];
    

提交回复
热议问题