[PLUG] Reading/Writing Binary Files with PHP

Bill Thoen bthoen at gisnet.com
Tue Aug 31 12:51:02 UTC 2004


On Tue, 31 Aug 2004, Bill Thoen wrote:

> Is it possible to write binary data to a file with PHP? When I try this:

Never mind... The answer is yes. I solved it by use of the pack() and
unpack() functions.  Like so:

  $data = 128;
  $fp = fopen ("test.bin", "w+b");
  fwrite ($fp, pack ("L", $data));

  // read it back...
  fseek ($fp, 0, SEEK_SET);
  $bindata = fread ($fp, 4);
  fclose ($fp);

  $arr = unpack ("Lval", $bindata);
  $data = $arr["val"];





More information about the PLUG mailing list