How to store binary data in PHP
People know all about storing binary data in database server as BLOBs. How would one accomplish the same thing in PHP?
In other开发者_Python百科 words, how do i store blobs in a php variable?
As PHP doesn't have Unicode support you can safely use normal strings as binary storage. Most (all?) functions are null-safe, too, so you shouldn't get any problems because of that either.
PS: Theoretically you could prefix all binary strings with b
(e.g. b'binary data'
). This is a forward compatability token to make sure that strings that expect to be handled as binary will really be handled so even than Unicode support is available.
Easy - store it in a string. You can use all the normal string functions (strlen, substr, etc) - just remember that the PHP string functions work in single byte units, e.g. substr($binstr, 0, 1)
gives you the first 8 bits of $binstr
Maybe as an array of bytes. After all binary data is nothing more.
精彩评论