简体   繁体   English

在PHP中打开.bat文件

[英]Open .bat file in php

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. 我想制作一个在线php hexeditor,用户在其中上传文件,服务器在其上执行指定的hexedit,然后将新文件保存在服务器上。 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. 我当时想我应该编写一个.bat文件,该文件在Windows上打开一个十六进制编辑器,执行指定的操作,然后返回新文件。 I could use the php function system(), or something like that. 我可以使用php函数system()或类似的东西。 Anybody know of a good way to do all this? 有人知道做这一切的好方法吗?

You can certainly achieve this using PHP only. 当然,您只能使用PHP来实现。

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. 查看fread函数,有一个示例显示了如何将文件读取为二进制文件。

Then use the bin2hex function which will give you a hexadecimal representation of binary data. 然后使用bin2hex函数,该函数将为您提供二进制数据的十六进制表示形式。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM