Validate that a file is a picture in PHP

后端 未结 5 695
孤城傲影
孤城傲影 2020-12-11 02:06

If a file is uploaded to the server, is there a way using PHP, to make sure that it\'s actually a picture and not just a file with a .jpg or .gif extension?

5条回答
  •  情歌与酒
    2020-12-11 02:20

    best way to check if file is an image

    function is_image($path)
    {
        $a = getimagesize($path);
        $image_type = $a[2];
    
        if(in_array($image_type , array(IMAGETYPE_GIF , IMAGETYPE_JPEG ,IMAGETYPE_PNG , IMAGETYPE_BMP)))
        {
            return true;
        }
        return false;
    }
    

    more: http://www.binarytides.com/php-check-if-file-is-an-image/

提交回复
热议问题