Q:

PHP's fread() is returning less than it advanced

With this code (and PHP 5.4.13 on a Linux box, Apache SAPI):
    $intOffset = 0;
    $intInterval = min(filesize(TL_ROOT . '/' . $this->strFile), 1024);
    $strBuffer = '';

    print 'INTERVAL: '.$intInterval.'<br />';
    print 'POS1: '.ftell($this->resFile).'<br />';
    // Read to delimiter
    do
    {
        $intOffset -= $intInterval;

        $fseek = @fseek($this->resFile, $intOffset, SEEK_END);
        print 'POS2: '.ftell($this->resFile).'<br />';
        print 'READING: '.abs($intOffset).'<br />';
        $add = @fread($this->resFile, abs($intOffset));
        print 'ADD LEN: '.strlen($add).'<br />';
        print 'POS3: '.ftell($this->resFile).'<br />';
        print 'OFFSET: '.$intOffset.'<br />';
        print 'BUF LEN (0): '.strlen($strBuffer).'<br />';
        $strBuffer = $add . $strBuffer;
        print 'BUF LEN (1): '.strlen($strBuffer).'<br />';
    }
    while ($fseek != -1 && ($pos = strpos($strBuffer, self::CENTRAL_DIR_END)) === false);

    print 'BUF LEN (2): '.strlen($strBuffer).'<br />';
    print 'POS: '.$pos.'<br />';

I get this debug output:
INTERVAL: 1024
POS1: 0
POS2: 1078538
READING: 1024
ADD LEN: 821
POS3: 1079562
OFFSET: -1024
BUF LEN (0): 0
BUF LEN (1): 821
BUF LEN (2): 821
POS: 785

WTF is going on here? Since when is it possible to return a string of length 821 from a read that advanced the file pointer 1024 bytes? Is there some multi-byte thing going on here? The file was opened with mode 'rb' and the manual says strlen() returns the length in bytes, so that shouldn't be the problem...

A:

is it possible to return a string of length 821 from a read that advanced the file pointer 1024 bytes? Is there some multi-byte thing going on here?

As you pointed, strlen counts bytes; however some extensions may overload the strlen function to count characters instead (e.g. mbstring )

