How do I get a file extension in PHP?

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

    Sometimes it's useful to not to use pathinfo($path, PATHINFO_EXTENSION). For example:

    $path = '/path/to/file.tar.gz';
    
    echo ltrim(strstr($path, '.'), '.'); // tar.gz
    echo pathinfo($path, PATHINFO_EXTENSION); // gz
    

    Also note that pathinfo fails to handle some non-ASCII characters (usually it just suppresses them from the output). In extensions that usually isn't a problem, but it doesn't hurt to be aware of that caveat.

提交回复
热议问题