文件确实以二进制形式存储在计算机上,但是1和0一起存储在8个组中(称为字节).现在,传统上,每个字节可以用ASCII字符表示,因为有256个可能的值可以用字节表示 – 这恰好与可用的不同ASCII字符的总数一致(这不是巧合)但实际上是按设计).
话虽如此,你从fread函数得到的是你应该得到的东西:即文件的内容.
如果你想看到1为0,你需要将你收到的每个字节打印成base 2表示.您可以使用base_convert等功能或编写自己的功能来实现.
$filename = "something.mp3";
$handle = fopen($filename, "rb");
$fsize = filesize($filename);
$contents = fread($handle, $fsize);
fclose($handle);
// iterate through each byte in the contents
for($i = 0; $i < $fsize; $i++)
{
// get the current ASCII character representation of the current byte
$asciiCharacter = $contents[$i];
// get the base 10 value of the current characer
$base10value = ord($asciiCharacter);
// now convert that byte from base 10 to base 2 (i.e 01001010...)
$base2representation = base_convert($base10value, 10, 2);
// print the 0s and 1s
echo($base2representation);
}
注意
如果你有一个1s和0s的字符串(一个字符的基数2表示),你可以将它转换回如下字符:
$base2string = '01011010';
$base10value = base_convert($base2string, 2, 10); // => 132
$ASCIICharacter = chr($base10value); // => 'Z'
echo($ASCIICharacter); // will print Z