开发者

What is the correct way of writing a single byte of data into a binary file using PHP?

开发者 https://www.devze.com 2023-02-19 08:44 出处:网络
When I write a string, PHP writes 4开发者_StackOverflow社区 bytes into the file. How do I write a single byte?

When I write a string, PHP writes 4开发者_StackOverflow社区 bytes into the file. How do I write a single byte?

Thanks


Open the handle in binary mode

foo@bar: /tmp/test > cat test.php 
<?php
 $fp = fopen('test.bin', 'w+b');
 fwrite($fp, 'a');
 fclose($fp);

foo@bar: /tmp/test > php test.php 
foo@bar: /tmp/test > ls -lh test.bin 
-rw-r--r--  1 foo  wheel     1B Mar 24 14:40 test.bin

just add b to the end of your fopen mode.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号