/** * Determines whether a file is text or binary by checking the first few bytes in the file. * The exact number of bytes is system dependent, but it is typically several thousand. * If every byte in that part of the file is non-null, considers the file to be text; * otherwise it considers the file to be binary. * * @param string $file * @return boolean */ public static function isBinary($file) { // open file $file = new File($file, 'rb'); // get block size $stat = $file->stat(); $blockSize = $stat['blksize']; if ($blockSize < 0) { $blockSize = 1024; } if ($blockSize > $file->filesize()) { $blockSize = $file->filesize(); } if ($blockSize <= 0) { return false; } // get bytes $block = $file->read($blockSize); return strlen($block) == 0 || preg_match_all('/\\x00/', $block, $match) > 0; }
/** * Determines whether a file is text or binary by checking the first few bytes in the file. * The exact number of bytes is system dependent, but it is typically several thousand. * If every byte in that part of the file is non-null, considers the file to be text; * otherwise it considers the file to be binary. * * @param string $file * @return boolean */ public static function isBinary($file) { // open file $file = new File($file, 'rb'); // get block size $stat = $file->stat(); $blockSize = $stat['blksize']; if ($blockSize < 0) $blockSize = 1024; if ($blockSize > $file->filesize()) $blockSize = $file->filesize(); if ($blockSize <= 0) return false; // get bytes $block = $file->read($blockSize); return (strlen($block) == 0 || strpos($block, "\0") !== false); }