The Linux Page

Bad move for PHP unpack()! So unpack() stopped working?

Today I noticed many errors on one of my websites. Looking closer into it, I noticed that the code for the mo_references Drupal module stopped working.

It took me some time, especially because the code seemed to work just fine as all the files could easily be displayed.

Actually, that was not the case. The unpack() character 'a', which I used, was transformed from a simple string that gets trimmed, to all the characters, including the NULL characters. Ouch! Now we have to use the 'Z' character instead.

I use it to unpack() a tar file by loading 512 bytes of data in a variable and then extracting the different fields with the unpack() command as in:

[...]
$block = gzread($gz, 512)
$header = unpack("Z100name/Z8mode/Z8uid/Z8gid/Z12size/Z12mtime/"
               . "Z8checksum/Z1type/Z100symlink/Z6magic/Z2temp/"
               . "Z32temp/Z32temp/Z8temp/Z8temp/Z155prefix/Z12temp", $block);
if($header['magic'] == 'ustar')
{
   // looks valid, go on
}

Source: https://www.php.net/manual/en/function.unpack.php

More: Learning PHP, MySQL & JavaScript