Duc Nguyen
Duc Nguyen

Reputation: 761

php read data from binary file

i cant find the answer for my problem. i had the code

$arr = array(pack("d",1324),pack("d",151),pack("d",8564));
file_put_contents('C:\\Users\\Duc Nguyen\\Desktop\\text.bin', $arr);

so i got a binary file. i used the code

$s = file_get_contents('C:\\Users\\Duc Nguyen\\Desktop\\text.bin');
foreach(unpack("d", $s) as $n) 
    echo $n;

to read it but it didnt work.can you show me how i can read the data from the file. i prefer not to use the serialize/unserialize function. thank you!

Upvotes: 2

Views: 99

Answers (1)

Rizier123
Rizier123

Reputation: 59681

You just used the wrong format for pack() and unpack(), just change d to d*. E.g:

$arr = array(pack("d*",1324),pack("d*",151),pack("d*",8564));
//...            v  ^               ^              ^
foreach(unpack("d*", $s) as $n) 

And a quote from the manual:

The repeater argument can be either an integer value or * for repeating to the end of the input data.

Upvotes: 1

Related Questions