views:

49

answers:

1

Hello, I want to make an online php hexeditor, where the user uploads a file, the server performs a specified hexedit on it, and then the new file is saved on the server. I was thinking that I should write a .bat file that opens a hex editor on windows, performs the specified actions, then returns the new file. I could use the php function system(), or something like that. Anybody know of a good way to do all this?

+1  A: 

You can certainly achieve this using PHP only.

What you need to do is:

  • read the file as binary
  • convert to hexadecimal representation
  • display it the way you like

Check out the fread function, there is an example showing how to read a file as binary.

Then use the bin2hex function which will give you a hexadecimal representation of binary data.

Here's a quick example:

<?php
$filename = "c:\\files\\somepic.gif";
$handle = fopen($filename, "rb");
$contents = fread($handle, filesize($filename));
fclose($handle);

$cols = 8;
$hex = bin2hex($contents);
$hex_split = str_split($hex,4*$cols);

foreach($hex_split as $h)
{
  $tmp = str_split($h, 4);
  foreach($tmp as $t)
    echo $t.' ';
  echo "\r\n";
}
?>

You will get for example:

d45b 0500 0000 0000 0c00 0000 0000 0000 
0000 0000 0000 0000 0100 0000 0000 0000 
0000 0000 0000 0000 0100 0000 0300 0000 
0000 0000 0000 0000 0000 0000 0000 0000 
e05b 0500 0000 0000 f400 0000 0000 0000 
0000 0000 0000 0000 0100 0000 0000 0000 
0000 0000 0000 0000
Weboide
Thanks a lot!! Can you help me out with writing and editing that file after I open it?
apple
Also, I get an error when I try to do this with a really big file. Could that just be a limitation of php?
apple