2

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!

2
  • didnt work What does that mean? Your code works fine for me. Commented Feb 21, 2015 at 16:39
  • i mean it returns only the first number which is 1324 Commented Feb 21, 2015 at 16:41

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

oh right. i should have read the manual carefully. thank you very much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.