Web lists-archives.com

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




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@xxxxxxxxxxxxxxxxxxxxxx
> 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