How to Check if File is ASCII or Binary in PHP
Since ASCII is just an encoding for text, with binary representation, not really. You could check that all bytes are less than 128, but even this wouldn't guarantee that it was intended to be decoded as ASCII. For all you know it's some crazy image format, or an entirely different text encoding that also has no use of all eight bits. It might suffice for your use, though. If you just want to check if a file is valid ASCII, even if it's not a "text file", it will definitely suffice.
this way it seems ok in my project:
function probably_binary($stringa) {
$is_binary=false;
$stringa=str_ireplace("\t","",$stringa);
$stringa=str_ireplace("\n","",$stringa);
$stringa=str_ireplace("\r","",$stringa);
if(is_string($stringa) && ctype_print($stringa) === false){
$is_binary=true;
}
return $is_binary;
}
PS: sorry, my first post, I wanted to add a comment to previous one :)
You should probably check the file's mimetype, but if you're willing to load the file into memory, maybe you could check to see if the buffer consists of all-printable-characters using something like:
<?php
$probably_binary = (is_string($var) === true && ctype_print($var) === false);
Not perfect, but might be helpful in some cases.
This only works for PHP>=5.3.0, and isn't 100% reliable, but hey, it's pretty darn close.
// return mime type ala mimetype extension
$finfo = finfo_open(FILEINFO_MIME);
//check to see if the mime-type starts with 'text'
return substr(finfo_file($finfo, $filename), 0, 4) == 'text';
http://us.php.net/manual/en/ref.fileinfo.php