Welcome! Log In Create A New Profile

Advanced

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

Posted by David Spector 
David Spector
[PHP] How to write binary data to a file?
December 13, 2016 05:41AM
Hi all,

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. Yes, the fopen was done with the
"b" mode specified.

It doesn't work this way in C or C++, and the PHP Manual says nothing
about this.

Web searches turn up nothing useful on this subject.

I have posted this question on three PHP fora, and have received only
unhelpful or irrelevant answers so far.

System: Windows 8.1, PHP 5.5.10

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
shiplu
Re: [PHP] How to write binary data to a file?
December 13, 2016 05:41AM
Its a matter of encoding. If have a data, it can be object, array, resource
type. You need to encode it first to string (stream of bytes) and pass it
to fwrite. If you use fwrite($hnd, 1), PHP will cast 1 as string and string
1 ("1") is in fact 0x31. PHP is doing fine here. If you have encoders
you'll also have decoders. Put your object in it, it should return the
encoded string. Write it using fwrite. For reading it back to memory, use
fread and then decode it back.

On Wed, Jul 27, 2016 at 12:10 AM, David Spector <news@springtimesoftware.com
> wrote:

> Hi all,
>
> 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. Yes, the fopen was done with the "b" mode specified.
>
> It doesn't work this way in C or C++, and the PHP Manual says nothing
> about this.
>
> Web searches turn up nothing useful on this subject.
>
> I have posted this question on three PHP fora, and have received only
> unhelpful or irrelevant answers so far.
>
> System: Windows 8.1, PHP 5.5.10
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


--
Shiplu.Mokadd.im
Unix Hacker

*Innovation distinguishes between follower and leader *
— Steve Jobs


*An expert is a person who has made all the mistakes that can be made in a
very narrow field.*— Neils Bohr
Christoph Becker
[PHP] Re: How to write binary data to a file?
December 13, 2016 05:41AM
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
shiplu
Re: [PHP] How to write binary data to a file?
December 13, 2016 05:41AM
On Wed, Jul 27, 2016 at 12:45 AM, David Spector <news@springtimesoftware.com
> wrote:

> fwrite($Res,(int)$IDByte,1);


This is wrong. You are first casting $IDByte as int. But fwrite accepts the
second parameter as string. So what it does is convert it back to string.
like "1".

I think what you are looking for is fprintf($Res, "%c", $IDByte). But it
assumes that $IDByte is lower than 255 otherwise it'll consume more than 1
byte. If its more than one byte how do align them. Do you write on left or
right. These are all encoding stuff.


--
Shiplu.Mokadd.im
Unix Hacker

*Innovation distinguishes between follower and leader *
— Steve Jobs


*An expert is a person who has made all the mistakes that can be made in a
very narrow field.*— Neils Bohr
David Spector
Re: [PHP] How to write binary data to a file?
December 13, 2016 05:41AM
Shiplu,

fprintf($Res, "%c", $IDByte);

worked. It actually wrote a byte containing 0b1. Whew.

Clearly, the problem was with fwrite converting its value to a
character. It shouldn't do this, IMO, since fwrite is documented as
working both for string and binary arguments (depending on the 'b' mode
of fopen).

Anyway, thank you for the solution, even though PHP is acting very
non-intuitively here.

I consider this question closed now: fwrite can only write strings.
Thanks to everyone for the responses.

David

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Christoph Becker
Re: [PHP] How to write binary data to a file?
December 13, 2016 05:41AM
On 27.07.2016 at 01:47, David Spector wrote:

> Clearly, the problem was with fwrite converting its value to a
> character. It shouldn't do this, IMO, since fwrite is documented as
> working both for string and binary arguments (depending on the 'b' mode
> of fopen).

The "b" mode only prevents line endings (CR/LF) to be converted to the
platform's convention. "binary safe" means that NUL characters (\0) can
be contained in the string (contrary to C).

> Anyway, thank you for the solution, even though PHP is acting very
> non-intuitively here.

However, it is rather consistent with itself here.

--
Christoph M. Becker

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
David Spector
Re: [PHP] How to write binary data to a file?
December 13, 2016 05:41AM
Dear Christoph Becker,

Thanks for the info about "b" mode and the definition of "binary safe".
I wish the Manual were clearer about these things. My misunderstandings
about fwrite were considerable. But this mailing list really works and I
thank everyone who helped me.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Sorry, only registered users may post in this forum.

Click here to login