Validate that a file is a picture in PHP

后端 未结 5 692
孤城傲影
孤城傲影 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:30

    Using (part) of the GD library.

    PHP: GD - Manual

    array getimagesize ( string $filename [, array &$imageinfo ] )
    

    The first element of the array will be 0 if there is no image. PHP: getimagesize

    If you don't have GD installed (most of the time you will), you can read the file header as Shane mentioned.

    EDIT: Actually, as Neal pointed out in the comments, the GD library is not even required to use this function. So use it.

提交回复
热议问题