Re: How to write binary data to a file?
| From: | Christoph Becker | Date: | Tue, 26 Jul 2016 22:59:30 +0000 |
| Subject: | Re: How to write binary data to a file? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-325664@lists.php.net to get a copy of this message | ||
On 27.07.2016 at 00:10, David Spector wrote:
> All I can find in the PHP Manual for writing raw bytes (unsigned
> integers) to a file is fwrite. But when I use it, it seems to write
> characters, not raw bytes. For example, the value 1 (0b1) is actually
> written as 0x31, not 0x01 as desired. […]
fwrite() accepts a string argument, and so other types are juggled to
string before they are written.
> It doesn't work this way in C or C++, and the PHP Manual says nothing
> about this.
Indeed. A link to pack()[1] appears to be useful. To write a byte
stream, you could use something like:
fwrite($stream, pack('C*', 1, 2, 3));
[1] <http://php.net/manual/en/function.pack.php>
--
Christoph M. Becker