Page 1 of 1

PHP - Reading Binary Files

Posted: Sun Sep 07, 2003 10:21 am
by Guest
Hi,

I am after a bit of help with reading data from a binary file in PHP.

I want to be able to write an array of numbers to a file, the specifically need to 'longs' so 4 bytes each, however when I try to read the data back I am getting the text version of it, how can I read an array from a file?

thanks guys.

Re:PHP - Reading Binary Files

Posted: Sun Sep 07, 2003 5:33 pm
by AGI1122
When writing, use mode "wb" and when reading use "rb". w is for write, r is for read, and the b's stand for binary files.

So use those for the second perameter of fopen();

Hope this helps.

Re:PHP - Reading Binary Files

Posted: Mon Sep 08, 2003 6:07 am
by Guest
Hi,

What I want to do is write data to a file, however I need it to be like a structure in c.

So for example I write a structure like this to disk:

struct mystruct {
int x;
char xx[64];
}

is this possible, I need to write records to a file and read them back, they will have fixed length strings, and integers.

I don't want to use a databse like mySql, is this possible in php?

or should I use Perl??

Please help, thanks.

PS. I know how to read and write binary files, I am just not sure how to read and write structures. I not even sure if PH has structures that are capable of this.

Re:PHP - Reading Binary Files

Posted: Tue Sep 09, 2003 2:15 am
by mystran
You have to decide on a fileformat, which is probably the hardest thing to do.

Say, you could do something like this:

write(my_fd, (char *) my_struct, sizeof(my_struct));

This will write the binary representation of the struct, which you can read back using read() in a similar way. The problem with this is that it breaks if you ever want to change the struct and then read old data, or you want to transfer the files to another computer. It also does not necessarily work with C++.

The right way of doing it is to write each member of the structure to the file separately. Also write some header before any structures so if you update your structures, you can use the headers (and a version flag in them) to figure out which version of the loader you want to use.

Another thing you might want to do is to convert multibyte values (like int) to byte-order to network byte-order (because it's easy to do in a portable way with htonl()) before saving them and back to host byte-order when loading.

This ofcourse won't work for structures with pointers. For pointers you need some more book-keeping so you can get them point to right place when loading.

Btw, I think you've just asked one of the questions that everybody asks at some point, but nobody has a good, generic answer for. I think that part of the popularity of XML becomes from the fact that it's awfully hard to deal with binary files if you ever need to extend your file format, or figure out what is wrong with it.

Re:PHP - Reading Binary Files

Posted: Tue Sep 09, 2003 5:19 am
by Guest
Hi,

I am now trying to use pack() and unpack() to handle the data, however what I am doing is not working.

Here is how I write the data:

Code: Select all

      $data = pack("A32A16LL", "32 Byte String!!", "16 Byte String!!", 1024, 2048);
      $fp = @ fopen("test.txt", "wb");
      if ($fp) {
         @ fwrite($fp, $data, 56);
         @ fclose($fp);
      }
This works fine, the data is written correctly.

Here is how I read the data:

Code: Select all

      $fp = @ fopen("test.txt", "r+b");
      if ($fp) {
         $data = @ fread($fp, 56);
         @ fclose($fp);
         list($str32, $str16, $long1, $long2) = unpack("A32A16LL", $data);

         echo $data;
         echo $str32 . $str16 . $long1 . $long2;
      }
OK, now things get interesting, first of all when I echo the $data without unpack()ing it, it displays it normally, the 56 bytes, however when I unpack() the data into the list() nothing is in the variables.

Can anyone see the problem? I am obviously missing something.

thanks.