What is the correct way of writing a single byte of data into a binary file using PHP?
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.
精彩评论