Larry S
Larry S

Reputation: 147

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 bytes into the file. How do I write a single byte?

Thanks

Upvotes: 0

Views: 245

Answers (1)

user667618
user667618

Reputation:

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.

Upvotes: 1

Related Questions