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:
PHP Code:
function get_byte($bdata)
{
if ( $bdata )
{
$bdata = unpack('Cmydata',$bdata);
return $bdata['mydata'];
}
else
return 0;
}
//THIS FUNCTION PREPARE DATA TO BE RIGHT-WRITTEN AS BINARY
function set_byte($data)
{
return pack('C',$data);
}
function get_long($bdata)
{
if ( $bdata )
{
$bdata = unpack('Lmydata',$bdata);
return $bdata['mydata'];
}
else
return 0;
}
function set_long($data)
{
return pack('L',$data);
}
$src;
$dst;
$tmp;
$bytes_written;
$total_bytes;
$data;
$cancel;
$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) )
{
fseek($tmp,0);
if ( get_byte(fread($tmp,1)) == 3 )
{
fclose($dst);
fclose($tmp);
unlink($_FILES['archivo']['name']);
unlink('tmp_file.bin');
$cancel = true;
break;
}
$bytes_written = fwrite($dst, $data);
$total_bytes += $bytes_written;
fseek($tmp,2);
fwrite($tmp,set_long($total_bytes)); // WRITE TOTAL BYTES WRITTEN
fseek($tmp,6);
$num = get_long(fread($tmp, 4)); // RETRIEVES BYTES WRITTEN SINCE LAST STATUS REQUEST
$bytes_written += $num; // ADD TO BYTES WRITTEN
fseek($tmp,6);
fwrite($tmp,set_long($bytes_written)); // WRITE BYTES WRITTEN SINCE LAST STATUS REQUEST
}
fclose($src);
if ( !$cancel )
{
fseek($tmp,0);
fwrite($tmp,set_byte(2)); // SET STATE TO FINISHED
fclose($dst);
}
Regards folks.
Bookmarks