Current Location: Home> Latest Articles> How to write data to a binary file using pack() with file_put_contents()?

How to write data to a binary file using pack() with file_put_contents()?

M66 2025-06-03

In PHP, the combination of the pack() function and file_put_contents() function is very practical when processing binary data. pack() can convert data into binary strings in the specified format, while file_put_contents() is responsible for writing these binary data to a file. This article will explain in detail how to use these two functions to write structured data into a binary file.

1. Introduction to pack() function

The pack() function converts variables into binary strings according to the format code. It supports a variety of format codes, such as:

  • c : Signed characters (1 byte)

  • C : Unsigned characters (1 byte)

  • s : signed short integer (2 bytes)

  • S : Unsigned short integer (2 bytes)

  • i : signed integer (4 bytes)

  • I : Unsigned integer (4 bytes)

  • f : floating point number (4 bytes)

  • d : double precision floating point number (8 bytes)

For more formats, please refer to the official PHP documentation.

2. Introduction to file_put_contents() function

The file_put_contents() function is used to write a string to a file. The file content will be overwritten by default, or it can be set to append mode.

3. Sample code: Write structured data to binary files

Suppose we want to write a data structure that contains ID (4-byte integer), status (1-byte character), floating-point value (4-byte floating-point number).

 <?php
// Data preparation
$id = 123456;           // Integer
$status = 1;            // state,1字节无符号Integer
$value = 3.14159;       // Floating point number

// use pack() Pack data by format
// Format description:I - Unsigned integer,C - Unsigned characters,f - Floating point number
$binaryData = pack('ICf', $id, $status, $value);

// Write to binary files
file_put_contents('data.bin', $binaryData);

echo "数据已Write to binary files。\n";
?>

After running the above code, a file named data.bin will be generated in the current directory, and the file content is the corresponding binary data.

4. Example of reading binary files

To verify whether the write is successful, we can use file_get_contents() to read the file content and parse it with unpack() :

 <?php
// Read binary files
$data = file_get_contents('data.bin');

// Unpacking data
// The format should be consistent with the writing time
$result = unpack('Iid/Cstatus/fvalue', $data);

print_r($result);
?>

The output result should be:

 Array
(
    [id] => 123456
    [status] => 1
    [value] => 3.14159
)

5. Things to note

  1. Byte order : pack() uses machine byte order by default. If it is transmitted across platforms or networks, it is recommended to specify the byte order format (such as N is a network byte order unsigned integer).

  2. Format matching : The formats of pack() and unpack() must correspond one by one, otherwise an error will be parsed.

  3. File permissions : Make sure PHP has sufficient permissions to operate the target folder when writing files.