What happens with
$fp = fopen('foo.bin', 'wb');
$written = fwrite($fp, $str);
if (strlen($str) != $written)
{
echo 'Not written', "\n";
}
Assuming $str is a binary string. The above code works just fine.
If it's a unicode string:
Short version: Don't do that.
Writing a unicode string to a binary mode file yields the U16 character
stream that the string is internally made up of, and yes, in this case
strlen($str) will not equal $written, but it's important to note that doing
this *IS* wrong. What you should be doing, if you want the U16 that the
string is encoded as, is to open the file for writing explicitly set the
encoding. This can be done by either:
$fp = fopen('foo.txt', 'w');
stream_encoding($fp, 'utf16');
Or any of the context/filter based methods given at:
http://blog.libssh2.org/index.php?/archives/6-PHP6-Streams-Update.html
(This'll all make it into the manual eventually, just saving it for now)
If you're going to (for some innane reason) switch between encodings as you
write to a file (there's never a good reason for this), you can use the
filter approach and just swap unicode.to.* filters in and out as needed.
-Sara
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php