PHP Upload File Validation

前端 未结 2 557
时光说笑
时光说笑 2020-12-17 00:56

I am creating file upload script and I\'m looking for the best techniques and practices to validate uploaded files.

Allowed extensions are:

$allowed_         


        
2条回答
  •  心在旅途
    2020-12-17 01:44

    If you want to validate images, a good thing to do is use getimagesize(), and see if it returns a valid set of sizes - or errors out if its an invalid image file. Or use a similar function for whatever files you are trying to support.

    The key is that the file name means absolutely nothing. The file extensions (.jpg, etc), the mime types... are for humans.

    The only way you can guarantee that a file is of the correct type is to open it and evaluate it byte by byte. That is, obviously, a pretty daunting task if you want to try to validate a large number of file types. At the simplest level, you'd look at the first few bytes of the file to ensure that they match what is expected of a file of that type.

提交回复
热议问题