How to check the content of an uploaded file without relying on its extension?

前端 未结 5 1863
天命终不由人
天命终不由人 2021-01-06 07:08

How do you go about verifying the type of an uploaded file reliably without using the extension? I\'m guessing that you have to examine the header / read some of the bytes,

5条回答
  •  不要未来只要你来
    2021-01-06 07:55

    Here's a quick-and-dirty response to the followup question you posted:

    byte[] jpg = new byte[] { 0xFF, 0xD8, 0xFF, 0xE0 };
    bool match = true;
    for (int i = 0; i < jpg.Length; i++)
    {
        if (jpg[i] != b[i])
        {
            match = false;
            break;
        }
    }
    

提交回复
热议问题