提问人:Thomas Miller 提问时间:1/25/2023 最后编辑:Thomas Miller 更新时间:1/30/2023 访问量:55
使用 fwrite php 替换文件中的十六进制值
Replace HEX value within the file using fwrite php
问:
我正在尝试使用PHP在特定偏移量上修改文件中的十六进制值。
$Offset1 = 30; //Offset 30 in the file
$valueinhex = dechex(90); //New value 90 in dec
$fh = fopen($current_file, 'wb');
fseek($fh, $Offset1);
fwrite($fh,$valueinhex);
fclose($fh);
我的问题是文件被所有内容都淘汰了,直到 offset1 有 00 的偏移量 1 是 90,这是 eof。就像 fseek 根本不起作用。
我认为问题出在 fwrite 中,它只写我的值而不是 current_file+值嗯
谢谢
-编辑-
让我重写问题:
I have File1.bin which has content:
01 02 03 04 05 06 07 08 09 0A
i want to edit 6th byte in this file to
01 02 03 04 05 FF 07 08 09 0A
and save it as File2.bin```
答:
0赞
Thomas Miller
1/29/2023
#1
解决方案在这里:
$current_file = "File1.bin";
$newfile = "File2.bin";
$Offset1 = 30;
$valueinhex = dechex(90);
$fh = fopen($newfile, 'wb+');
rewind($fh);
$content = file_get_contents($current_file, false, NULL, 0, filesize($current_file));
fwrite($fh, $content);
fseek($fh, $Offset1);
fwrite($fh, pack('H*', $valueinhex), 2);
rewind($fh);
fclose($fh);
一切正常:)
评论