---------- Forwarded message ----------
From: "Philip W." <[EMAIL PROTECTED]>
To: php-general@lists.php.net
Date: Mon, 01 Jan 2007 15:15:07 -0500
Subject: Writing Binary
Hello Everyone,
   I'm trying to write a PHP script that opens a binary file, reads the
binary in chunks, checks those chunks for a certain binary string, and
then if the string is found, it replaces the string with another binary
string.  The script should then a copy of the file, including the
changed bits.  But for some reason, the script is not replacing the
binary string.

Here's the source code:

<?php

$in = fopen("C:\NeighborhoodManager.package", "rb");
$out = fopen("C:\NeighborhoodManager2.package", "w");
$count = 0;

while (!feof($in)) {
$count++;
$buffer = fread($in,50000);
if

(eregi("0101001101110100011100100110000101101110011001110110010101110100011011110111011101101110",$buffer))
{

$buffer2=eregi_replace("0101001101110100011100100110000101101110011001110110010101110100011011110111011101101110","0101011001101001011000110111010001101111011100100110100101100001",$buffer);
}
else
{
   $buffer2 = $buffer;
}
fwrite($out,$buffer2);
}
fclose($out);
fclose($in);
print("About ".($count*50000)." bytes read.\n");

?>

The resulting file is not changed at all, the size, md5 sums, and
contents are the same as the original file.  The original file is only
40,960 bytes on the disk, so the script reads the file in one pass. I am
sure the binary string is in the original file, and I have tried reading
the file into chunks as small as 64 bytes.  I am at a loss as to what is
happening, so I would appreciate any help.

Sincerely,
Philip W.



This article might help you out
http://www.onlamp.com/pub/a/php/2002/12/12/php_foundations.html

--
Regards
Fahad Pervaiz
www.ecommerce-xperts.com

Reply via email to