473,480 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 8659
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2981
by: Peter Abel | last post by:
Hi all, I'm working under W2k with Python 2.2.2 (#37, Oct 14 2002, 17:02:34) on win32 I have a file *test_data.txt* with the following content: 0123456789 0123456789 abcdefghi...
13
15189
by: yaipa | last post by:
What would be the common sense way of finding a binary pattern in a ..bin file, say some 200 bytes, and replacing it with an updated pattern of the same length at the same offset? Also, the...
3
399
by: DJTN | last post by:
I have created 2 vb.net applications in VS 2002, a server and a client using the .net.sockets namespace. I can connect and receive data fine but the client cannot tell when it has recived all the...
8
1769
by: siliconwafer | last post by:
Hi All, If I open a binary file in text mode and use text functions to read it then will I be reading numbers as characters or actual values? What if I open a text file and read it using binary...
7
6025
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
4
9530
by: Florence | last post by:
How can a binary file be distinguished from a text file on Windows? Obviously I want a way that is more sophisicated that just looking at the dot extention in the filename. I want to write...
10
13836
by: chat | last post by:
Hi, I know that text file ended with EOF mark but there is no mark for binary file. So, the problem is how do we know the end of binary file is reach? This code can tell us when the end of file...
7
2605
by: lawrence k | last post by:
I've got a music studio for a client. Their whole studio is run with Macintosh computers. Macintosh computers allow file names to have open white spaces, such as "animal hospital.mp3". I have a...
3
2882
by: Magdoll | last post by:
I was trying to map various locations in a file to a dictionary. At first I read through the file using a for-loop, but tell() gave back weird results, so I switched to while, then it worked. ...
0
7055
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6920
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7110
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6763
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7030
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5367
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3015
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
574
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
210
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.