바이너리(또는 TEXT 파일)을 php 파일로 변환하는 함수입니다.

<?php 
 function bin2php($input_file, $output_file){ 
    $i = file_get_contents($input_file); 
  
    $b = array(); 
    $x = 0; $y = 0; 
  
    for ($c=0; $c < strlen($i); $c++){ 
        $no = bin2hex($i[$c]); 
        $b[$x] = isset($b[$x]) ? $b[$x].'\\x'.$no : '\\x'.$no; 
        if ($y >= 10){  
            $x++; $y = 0; 
        } 
        $y++; 
    } 
  
    $output = "<"."?php\n"; 
    $output .= "\$f=\""; 
    $output .= implode("\";\r\n\$f.=\"", $b); 
    $output .= "\";\nprint \$f;"; 
    $output .= "\n?>"; 
  
    $fp = fopen($output_file, 'w+'); 
    fwrite($fp, $output); 
    fclose($fp); 
 } 

 bin2php('test.png', 'test.php'); 
 /* 
 출력: 
 <?php 
 $f="\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00"; 
 $f.="\x0d\x49\x48\x44\x52\x00\x00\x00\x60\x00"; 
 $f.="\x00\x00\x14\x08\x02\x00\x00\x00\xfd\x49"; 
 $f.="\xaf\xb8\x00\x00\x00\x09\x70\x48\x59\x73"; 
 $f.="\x00\x00\x0b\x13\x00\x00\x0b\x13\x01\x00"; 
 .... 
 ?>
 */
 ?>

출처: http://www.jonasjohn.de/snippets/php/bin2php.htm

0 댓글