Checking For Binary Files

Want to share what MODs you've gotten to work with EM? Happy about all the time your're saving? Want to say "thanks"? Here's the place.
Forum rules
DO NOT give out any FTP passwords to anyone! There is no reason to do so! If you need help badly enough, create a temporary FTP account that is restricted to only the files that you need help with and give the information for that. Giving out FTP information can be very dangerous!
Locked
User avatar
GPHemsley
Registered User
Posts: 1617
Joined: Fri Apr 18, 2003 4:01 am
Location: Long Beach, NY
Contact:

Checking For Binary Files

Post by GPHemsley »

I found this function why browsing PHP.net. It supposedly checks if a file is binary, which could be helpful for determining which mode to upload in. Here it is, in full:
jdhurn at uberidx dot com
08-Mar-2003 05:18


This is a function I use to determine if a file contains Binary information. I use this for my search engine so that it doesn't try to index files like .zip or .mp3 or any other file that doesn't contain readable information. It makes use of the Character Type Extension if it's loaded, if it's not then it uses Regular Expressions.

Code: Select all

function is_binary($link)
{
     $tmpStr  = '';
     @$fp    = fopen($link, 'rb');
     @$tmpStr = fread($fp, 256);
     @fclose($fp);

     if($tmpStr != '')
     {
         $tmpStr = str_replace(chr(10), '', $tmpStr);
         $tmpStr = str_replace(chr(13), '', $tmpStr);

         $tmpInt = 0;

           for($i =0; $i < strlen($tmpStr); $i++)
         {
               if( extension_loaded('ctype') )
               {
                   if( !ctype_print($tmpStr[$i]) )
                         $tmpInt++;
               }
               else
               {
                   if( !eregi("[[:print:]]+", $tmpStr[$i]) )
                         $tmpInt++;
               }
           }

           if($tmpInt > 5)
               return(0);
           else
               return(1);
     }
     else
           return(0);
}

Locked