469,934 Members | 2,757 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,934 developers. It's quick & easy.

How to tell if a file is binary

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

Dec 6 '05 #1
4 8142
You might try using file command on linux systems
--
Geeks Home
www.fahimzahid.com


"comp.lang.php" <ph**************@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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

Dec 6 '05 #2
* Determine if a file is binary. Useful for doing file content
editing Problem is that the results are completely backwards: 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?


Here is another suggestion:
<http://groups.google.co.uk/group/comp.lang.php/browse_thread/thread/1d01eb12555a940d/cbf2065e8238ac45#cbf2065e8238ac45>

---
Steve

Dec 6 '05 #3
Thanx but that was not enough information for me.

1) Is the "diff tool" PHP, Linux, Windows, UNIX, Perl, ...??
2) Is there a comprehensive way of determining exactly how many bytes
you read into this diff tool (e.g. Windows: 1024? Linux: 256? FreeBSD:
512)

Thanx
Phil

Steve wrote:
* Determine if a file is binary. Useful for doing file content
editing

Problem is that the results are completely backwards:

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?


Here is another suggestion:
<http://groups.google.co.uk/group/comp.lang.php/browse_thread/thread/1d01eb12555a940d/cbf2065e8238ac45#cbf2065e8238ac45>

---
Steve


Dec 7 '05 #4
Thanx but that was not enough information for me. 1) Is the "diff tool" PHP, Linux, Windows, UNIX, Perl, ...??
2) Is there a comprehensive way of determining exactly how many bytes
you read into this diff tool (e.g. Windows: 1024? Linux: 256? FreeBSD:
512)


I wasn't suggesting you use diff itself, just create a test in PHP
using a similar algorithm. There's no definitive number of bytes to
test, as it is not an exact science. The more bytes you test, the more
reliable the result.

---
Steve

Dec 7 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Peter Abel | last post: by
8 posts views Thread by siliconwafer | last post: by
7 posts views Thread by John Dann | last post: by
4 posts views Thread by Florence | last post: by
3 posts views Thread by Magdoll | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.