Web lists-archives.com

[PHP] Re: How to write binary data to a file?




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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php