I can see that most of you didn’t understand the question 🙂 (question was how to validate binary data in buffer, not a file on disk).
I had same problem, and resolved it with:
$finfo = new finfo(FILEINFO_MIME_TYPE);
$mimeType = $finfo->buffer($rawImage);