mwotton
mwotton

Reputation: 2200

Convert file to binary string with PHP

I'm looking to convert a file to a binary string, preferably using PHP. Is this possible?

MORE INFO: I'd like to have users upload a small file through a form, and return the binary representation of that file as a binary string - i.e ones and zeros.

Upvotes: 0

Views: 3614

Answers (1)

zerkms
zerkms

Reputation: 254916

$str = 'hello world';
$length = strlen($str);
$result = '';

for ($i = 0; $i < $length; $i++) {
    $result .= str_pad(decbin(ord($str[$i])), 8, '0', STR_PAD_LEFT);
}

echo $result;

Upvotes: 4

Related Questions