> > > following problem appeared on PHP 3.0.16 on LINUX
> > > I have a binary file, where I want to replace some letters
> > > I do the following:
> > > $filename = "xyz.sdc";
> > > $fd = fopen( $filename, "r+b" ); // also tried "r"
> > > $x=filesize( $filename ); // this shows the correct filesize 22528 byte
> > > $contents = fread( $fd, $x );
> > > fclose( $fd );
> > > $y=strlen($contents); // The first mistake $y = 32956 byte
> > > echo "BEFORE<br>".$contents;
> > > $contents=str_replace("UUUUUU","123456", $contents);
> > > echo "AFTER<br>".$contents;
> > > It seems, that php converts HEX 00 00  to HEX 5C 30 just when it reads the
> > > data in
> > > memory because at the BEFORE echo the data will be shown as \0 (hex 00
> > 00 ).
> > > The filesize will be correct when I save the str_replaced text and the
> > text
> > > will be cahnged
> > > correct.
> > > Any idea, where this comes from??
> >Let's start with the fopen(), where the mode expression might be causing
> >problems - should it be "rb"?
> >http://www.php.net/manual/en/function.fopen.php
> >If the 'binary' component hasn't been recognised it might explain the
> >"first mistake".
> >The ensuing problems may even stem from there...
> Thanks DLNeil,
> I tried "rb" "r" "r+b"(C-style) always the same :(
> Any other suggestions ?

=not really, but (here's the end of a beautiful friendship) I've managed to get the 
code to work as required (on my
WinNT box - I thought it was us Windows users who always have to accept 
second-best!?). I took your code, added various
extras and amended things that wouldn't run on my system - my added code is indented 
and your original commented out
where necessary - see section 1 below. I then threw my c:\boot.ini file (287b used, 
512b allocated) against it, and
asked that the three appearances of the word "multi" be replaced by "123", which 
caused a 6-byte contraction - see
section 2 below. I don't think anything I changed has altered the original logic.

=Despite my earlier observations, my system doesn't care whether the fopen() mode is 
set to "r", "rb", or "r+b". However
as soon as I shifted from the ultra-short boot.ini to using the binary ntldr file I 
noticed that PHP baulked at the
backslash (fine with forward slash though).

=a few thoughts:
1 the file must be local to be able to use filesize() (it may not be a remote URL for 
example) - but then you say the
filesize is initially correct so that's not it.
2 the replace function is case sensitive (I can't see your data file, so is that a 
possible problem?).
3 the manual does say that the replace function was buggy in older versions of PHP.
4 I've added some error checks. If you run them against your data, do they throw out 
anything?

=let us know,
=dn

----------

<html>
<head><title>Oliver/[PHP]</title></head>
<body>
<?php

//$filename = "xyz.sdc";
//$fd = fopen( $filename, "r+b" ); // also tried "r"
   $filename = "c:\boot.ini";
   $fd = fopen( $filename, "rb" );
   if (!( $fd ) ) echo "Could not open file: $filename";
$x=filesize( $filename ); // this shows the correct filesize 22528 byte
   echo "filesize=$x";
   $contents  = fread( $fd, $x );
   if (!$contents) echo "FileSysRead failed~<BR>";
//$contents = fread( $fd, $x );
fclose( $fd );
$y=strlen($contents); // The first mistake $y = 32956 byte
echo "<br>BEFORE<br>".$contents;
   echo "<br>length=$y";
   $contents=str_replace("multi","123", $contents);
//$contents=str_replace("UUUUUU","123456", $contents);
echo "<br>AFTER<br>".$contents;
   echo "<br>length=".strlen($contents);

?>
</body>
</html>

----------

filesize=287
BEFORE
[operating systems] multi...etc...
length=287
AFTER
[operating systems] 123...etc...
length=281

----------



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to