Sar*_*raz 18
你正在寻找fread功能.
fread - 二进制安全文件读取
例:
$filename = "c:\\files\\somepic.gif";
$handle = fopen($filename, "rb");
$contents = fread($handle, filesize($filename));
fclose($handle);
Run Code Online (Sandbox Code Playgroud)
注意:
在区分二进制文件和文本文件(即Windows)的系统上,必须打开文件,并在fopen()模式参数中包含"b".
vba*_*osh 16
file_get_contents够好了.它似乎以二进制模式读取文件.我做了一个小PHP脚本来检查这个.没有生成MISMATCH消息.
<?php
foreach (glob('/usr/bin/*') as $binary) {
$php = md5(file_get_contents($binary));
$shell = shell_exec("md5sum $binary");
if ($php != preg_replace('/ .*/s', '', $shell)) {
echo 'MISMATCH', PHP_EOL;
}
else {
echo 'MATCH', PHP_EOL;
}
echo $php, ' ', $binary, PHP_EOL;
echo $shell, PHP_EOL;
}
Run Code Online (Sandbox Code Playgroud)
以下注释来自手册:
注意:此函数是二进制安全的.
尝试这个
$handle = @fopen("/path/to/file.bin", "rb");
if ($handle) {
while (!feof($handle)) {
$buffer[] = fgets($handle, 400);
}
fclose($handle);
$buffer[0][0] = chr(hexdec("FF")); // set the first byte to 0xFF
}
// convert array to string
Run Code Online (Sandbox Code Playgroud)