I have a website that works with text files that users upload, to make sure that they are actually text files i check the mime type in PHP like this:
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mimeType = finfo_file($finfo, $filepath);
finfo_close($finfo);
Which works fine most of the time. The problem is that sometimes files are uploaded that contain a few control characters (non-printable characters like nul or stx). Trying to get the mime type of these files always returns application/octet-stream. For example, a textfile i have that is 560 lines long, contains one nul character on line 12, and therefor gets indentified as application/octet-stream
Is there any safe and reliable way to check if an uploaded file is a text file when detecting the mime type doesn't work?