[thelist] Binary file manipulation in PHP

Rick den Haan rick.denhaan at gmail.com
Thu Feb 14 03:18:10 CST 2008

Jeffrey Barke wrote:
> I only worked with binary files once and it was experiment, but I
> believe you need the pack() and unpack() functions:
> <http://us3.php.net/manual/en/function.pack.php>
> <http://w-shadow.com/blog/2006/10/17/processing-binary-files-in-php/>

Thanks! The unpack() function did the trick.

$binVersion = fread($rHandler, 2);
$aVersion = unpack('n', $binVersion);

This gives an array, telling me the version number is 2. Which might very
well be more correct than using the 'v' format, which gives version 512
(which doesn't exist).

Now I just have to figure out which types the various bytesets are, but now
that I have this function, I'm sure I can get the file contents back.

For reference, I tried changing the character set, as Ron suggested, to a
lot of different types, but all of them gave me various incarnations of

unpack() appears to solve my problem.


More information about the thelist mailing list