Search code examples
phpfile-iobinaryfiles

Which PHP function to use to read a binary file into a string?


Which PHP function to use to read a binary file into a string?


Solution

  • Try this

    $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