|
Posted by comp.lang.php on 12/06/05 20:53
I borrowed the following function from the PHP manual user notes:
[PHP]
if (!function_exists('is_binary')) {
/**
* Determine if a file is binary. Useful for doing file content
editing
*
* @access public
* @param mixed $link Complete path to file (/path/to/file)
* @return boolean
* @link http://us3.php.net/filesystem#30152
* @see link user notes regarding this created function
*/
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++;
} elseif (!eregi("[[:print:]]+", $tmpStr[$i])) {
$tmpInt++;
}
}
if ($tmpInt > 5) return(0); else return(1);
} else {
return(0);
}
}
}
[/PHP]
Problem is that the results are completely backwards:
[PHP]
print_r(is_binary("/path/to/my/image.jpg")); // RETURNS 0
print_r(is_binary("/path/to/my/text.txt")); // RETURNS 1
[/PHP]
It's basically saying that binary files are ASCII and all ASCII files
are binary! Is there a better function out there that can tell me if a
file is binary or not?
Thanx
Phil
Navigation:
[Reply to this message]
|