Pwnna
Pwnna

Reputation: 9538

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 words, how do i store blobs in a php variable?

Upvotes: 3

Views: 5542

Answers (3)

Robin
Robin

Reputation: 4260

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

Upvotes: 1

NikiC
NikiC

Reputation: 101926

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.

Upvotes: 2

Hyperboreus
Hyperboreus

Reputation: 32429

Maybe as an array of bytes. After all binary data is nothing more.

Upvotes: 0

Related Questions