二进制文件编辑器

  本程序是直接在浏览器里面编写二进制文件,界面左半部分是十六进制代码,右半部分是相应的字符,可以自由编辑保存。
  程序文件只有一个,存为任意*.php均可。

<?
/**********************************************************************
    Written by caocao
    [email protected]
    http://caocao.oso.com.cn
**********************************************************************/
  
if (!$action)
  {
?>
<html>
<head>
<title>二进制文件编辑器</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<frameset rows="0,*" cols="*" frameborder="NO" border="0" framespacing="0"> 
  <frame name="action" noresize scrolling="NO" src="<? echo $PHP_SELF."?action=2";?>">
  <frame name="main" src="<? echo $PHP_SELF."?action=1";?>">
</frameset>
<noframes>
  <body bgcolor="#FFFFFF">
  <p>本页使用页面框架,抱歉您的浏览器不支持</p>
  </body>
</noframes>
</html>
<?
    
exit;
  }
  if (
$action==1)
  {
?>
<html>
<head>
<title>二进制文件编辑器</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body bgcolor="#FFFFFF">
<table width="760" border="0" cellspacing="0" cellpadding="0" height="25">
  <tr>
    <td>
      <div align="center"><b>二进制文件编辑器</b></div>
    </td>
  </tr>
</table>
<table width="760" border="0" cellspacing="0" cellpadding="0">
  <tr> 
    <td>
      <form name="form" method="post" action="<? echo $PHP_SELF."?action=2";?>" target="action">
        <div align="center">
          <textarea name="code1" cols="32" rows="20"></textarea>
          <textarea name="code2" cols="16" rows="20"></textarea>
          <hr width="400">
          <input type="text" name="path" size="40">
          <input type="hidden" name="status" value="1">
          <br>
          <input type="submit" name="save" value=" 保 存 " onclick="form.status.value=1;">
           
          <input type="submit" name="read" value=" 读 取 " onclick="form.status.value=0;">
        </div>
      </form>
    </td>
  </tr>
</table>
</body>
</html>
<?
    
exit;
  }
  if (
$action==2)
  {
    
$javascript="";
    if (
$path)
    {
      if (
$status)
      {
        if (
$code1)
        {
          
$fp=fopen($path,"wb");
          for (
$i=0;$i<strlen($code1)/2;$i++)
          {
            
fwrite($fp,chr(hexdec(substr($code1,$i*2,2))),1);
          }
          if (
fclose($fp))
          {
            
$javascript="parent.main.form.code1.value=\"保存成功\";";
            
$javascript.="parent.main.form.code2.value=\"\";";
          }
          else
          {
            
$javascript="parent.main.form.code1.value=\"保存失败\";";
          }
        }
        else
        {
          
$javascript="parent.main.form.code1.value=\"没有数据\";";
        }
      }
      else
      {
          
$code1="";
          
$code2="";
          if (
$fp=fopen($path,"rb"))
          {
            
$file_data=fread($fp,filesize($path));
          
fclose($fp);
          for (
$i=0;$i<strlen($file_data);$i++)
          {
            
$code1.=bin2hex(substr($file_data,$i,1));
          }
          
$code2=ereg_replace("%+[0-9A-F]{2}",".",rawurlencode($file_data));
          
$javascript="parent.main.form.code1.value=\"".$code1."\";";
          
$javascript.="parent.main.form.code2.value=\"".$code2."\";";
        }
        else
        {
          
$javascript="parent.main.form.path.value=\"文件不存在\";";
        }
      }    
    }
    if (!
$path&&($read||$save))
    {
      
$javascript="parent.main.form.path.value=\"请输入文件名和路径\";";
    }
?>
<script language="javascript">
<? echo $javascript;?>
</script>
<?    
      
exit;
  }
?>