473,554 Members | 3,197 Online
Bytes | Software Development & Data Engineering Community
+ 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_exis ts('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#3015 2
* @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_load ed('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_bina ry("/path/to/my/image.jpg")); // RETURNS 0
print_r(is_bina ry("/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 8676
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.goo glegroups.com.. .
I borrowed the following function from the PHP manual user notes:

[PHP]
if (!function_exis ts('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#3015 2
* @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_load ed('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_bina ry("/path/to/my/image.jpg")); // RETURNS 0
print_r(is_bina ry("/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.c o.uk/group/comp.lang.php/browse_thread/thread/1d01eb12555a940 d/cbf2065e8238ac4 5#cbf2065e8238a c45>

---
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.c o.uk/group/comp.lang.php/browse_thread/thread/1d01eb12555a940 d/cbf2065e8238ac4 5#cbf2065e8238a c45>

---
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
2997
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 ABCDEFGHIJKLMNOPQ
13
15207
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 pattern can occur on any byte boundary in the file, so chunking through the code at 16 bytes a frame maybe a problem. The file itself isn't so large,...
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 data from the server and close the file. I'm sending multiple request for data and I do not want to close the connecting everytime the entire file...
8
1779
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 read functions. -Siliconwafer
7
6045
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 populate a series of structures of specified variable composition. I have the structures created OK, but actually reading the files is giving me an...
4
9535
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 code that processes all text files in a directory but leaves binary files alone. -- http://www.florencesoft.com
10
13844
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 is reach int ch; FILE *fp;
7
2615
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 download script, so customers on the website can download MP3s to their harddrive (rather than merely listen to it in their browsers):
3
2896
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. The for-loop version was something like: d = {} for line in f: if line.startswith('>'): d = f.tell() And the while version was:
0
7583
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7507
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7542
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7873
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5424
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5143
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
825
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.