Extremely weird error writing binary data (only with IE)
Hi to everyone. I was kind of surprised when I tested my upload application with Internet Explorer 6 and it wasn't working since it was in firefox. The error is quite simple, but finding a solution about it I just guess that it is not. My application works like this: there's a php file wich handles the upload. This file also writes some binary values representing relevant upload statistics into a tmp file, wich is actually read from a background process executed by ajax to track the progress of the upload. The structure of this file is the following:
Byte 0: defines the state of the upload. Three states are allowed 1 for copying, 2 for finished and 3 for cancelled
Bytes 1-4: long value. Represents how many bytes have been uploaded.
Bytes 5-9: long value. Represents how many bytes have been uploaded since the last status request. This value is exactly the bytes per second that have been uploaded.
Well, the thing is that my application when is executed with Internet Explorer 6 doesn't write binary data in a correct manner and it blows it off, specially when it writes byte 0 value, because all the logic is around the three states that can have that byte. The trouble is that it always writes a 2, on despites of anything I pass as an arg of the function. The last thing I want to say is that you remember that this trouble is only happening with Internet Explorer. With firefox all works as I expect. Here's my code:
if ( $bdata )
$bdata = unpack('Cmydata',$bdata);
//THIS FUNCTION PREPARE DATA TO BE RIGHT-WRITTEN AS BINARY
if ( $bdata )
$bdata = unpack('Lmydata',$bdata);
$src = fopen($_FILES['archivo']['tmp_name'], 'r');
$dst = fopen($_FILES['archivo']['name'], 'w');
$tmp = fopen('tmp_file.bin','wb+');
fwrite($tmp,set_byte(1)); // HERE IS WHERE I WRITE STATE BYTE. IT ALWAYS WRITES a 2.
$bytes_written = 0;
$total_bytes = 0;
$num = 0;
$cancel = false;
while ( $data = fread($src,1024) )
if ( get_byte(fread($tmp,1)) == 3 )
$cancel = true;
$bytes_written = fwrite($dst, $data);
$total_bytes += $bytes_written;
fwrite($tmp,set_long($total_bytes)); // WRITE TOTAL BYTES WRITTEN
$num = get_long(fread($tmp, 4)); // RETRIEVES BYTES WRITTEN SINCE LAST STATUS REQUEST
$bytes_written += $num; // ADD TO BYTES WRITTEN
fwrite($tmp,set_long($bytes_written)); // WRITE BYTES WRITTEN SINCE LAST STATUS REQUEST
if ( !$cancel )
fwrite($tmp,set_byte(2)); // SET STATE TO FINISHED
Last edited by mariano_donati; 08-13-2008 at 11:43 AM.
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)