php 从二进制文件读取数据

php read data from binary file

我找不到问题的答案。我有代码

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

所以我得到了一个二进制文件。我使用代码

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

阅读它,但它没有 work.can 你告诉我如何从文件中读取数据。我不想使用 serialize/unserialize 函数。 谢谢!

您刚刚使用了错误的 pack() and unpack() 格式,只需将 d 更改为 d*。例如:

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

以及手册中的引述:

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