Hello.. i'm using php on linux --version:
PHP 5.2.5 (cli) (built: Apr 25 2008 18:40:41)
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
and i'm experiencing unexpected behavior using fread with a binary
file.
Look at this code: i have a jpeg image of 2290 bytes long, but fread
cannot read it correctly, like fgetc does:
<code>
$data1 = fread(fopen($f, 'r'),2290);
$h = fopen( $f, 'r' );
while(!feof($h) )
$data2 .= fgetc($h);
echo "l1(".strlen($d ata1).") l2(".strlen($da ta2).")
size(".filesize ($f).")\n";
for($i=0;$i<229 0;$i++)
echo "Char($i) = d1(".ord($data 1[$i]).") d2(".ord($data 2[$i]).")
\n";
</code>
This is the output:
<output>
l1(2384) l2(2290) size(2290)
Char(0) = d1(255) d2(255)
Char(1) = d1(216) d2(216)
Char(2) = d1(255) d2(255)
Char(3) = d1(224) d2(224)
Char(4) = d1(92) d2(0)
Char(5) = d1(48) d2(16)
Char(6) = d1(16) d2(74)
Char(7) = d1(74) d2(70)
....
</output>
As you can see, the byte at position 4 is read as 0 (correctly, it is
0) with fgetc and as a 2-byte-long sequence 92-48 by fread.. resulting
in a corrupted file.
This behaviour is repeated whenever the file has a 0ed byte in its
content
Can anyone of you explain this ?..
Tnx
PHP 5.2.5 (cli) (built: Apr 25 2008 18:40:41)
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
and i'm experiencing unexpected behavior using fread with a binary
file.
Look at this code: i have a jpeg image of 2290 bytes long, but fread
cannot read it correctly, like fgetc does:
<code>
$data1 = fread(fopen($f, 'r'),2290);
$h = fopen( $f, 'r' );
while(!feof($h) )
$data2 .= fgetc($h);
echo "l1(".strlen($d ata1).") l2(".strlen($da ta2).")
size(".filesize ($f).")\n";
for($i=0;$i<229 0;$i++)
echo "Char($i) = d1(".ord($data 1[$i]).") d2(".ord($data 2[$i]).")
\n";
</code>
This is the output:
<output>
l1(2384) l2(2290) size(2290)
Char(0) = d1(255) d2(255)
Char(1) = d1(216) d2(216)
Char(2) = d1(255) d2(255)
Char(3) = d1(224) d2(224)
Char(4) = d1(92) d2(0)
Char(5) = d1(48) d2(16)
Char(6) = d1(16) d2(74)
Char(7) = d1(74) d2(70)
....
</output>
As you can see, the byte at position 4 is read as 0 (correctly, it is
0) with fgetc and as a 2-byte-long sequence 92-48 by fread.. resulting
in a corrupted file.
This behaviour is repeated whenever the file has a 0ed byte in its
content
Can anyone of you explain this ?..
Tnx
Comment