PHP Replace binary file at hex address

1.5k views Asked by At

I want to modify a binary file server side of aprox. 700kb to change an url in it than save it.

My first idea was to use bin to hex and preg_replace to replace url in binary.

The adress for the url in binary is always the same, but i need to change it every time my function is called.

Is there a better/faster way to do this ?

Example:

somesite.com/api***

I want to replace *** with some numbers from a var for example.

*** is between hex adress 00010edb-00010edd

Thanks!

1

There are 1 answers

3
umka On BEST ANSWER

If the string is at fixed position, you can write data directly:

$position=hexdec("00010edb"); // You have to pre-calculate it once
$data="some data"; // Replacement
if ($f=fopen("your_file", "r+")) {
    fseek($f, $position);
    fwrite($f, $data);
    fclose($f);
} else {
    echo "Can't open file";
}