How to find the extension of an image from path in PHP?

前端 未结 9 669
抹茶落季
抹茶落季 2021-01-04 12:52

Is there any standard function in PHP to find only extension of an image from the corresponding file path?

For example ff my image path is like \'/testdir/dir2/image

相关标签:
9条回答
  • 2021-01-04 13:30
    $ext = pathinfo(
        parse_url('/testdir/dir2/image.gif?foo=bar', PHP_URL_PATH), 
        PATHINFO_EXTENSION
    ); //$ext will be gif
    
    0 讨论(0)
  • 2021-01-04 13:31

    It's usually more desirable to detect the actual image type (not by extension but by its contents). For that, use getimagesize().

    0 讨论(0)
  • 2021-01-04 13:35

    I would recommend you perfect way

    $file_path = '/some/where/img.gif';  
    $info = new SplFileInfo($file_path);  
    $file_extension = $info->getExtension(); 
    var_dump($file_extension);
    

    for more detail here The SplFileInfo class

    I hope this will help you.

    Cheers!

    Mudassar Ali

    0 讨论(0)
提交回复
热议问题