Bin2PHP

Converts a binary file or a text file to an php file

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);
}
Snippet Details



bin2php('test.png', 'test.php');<br/>Create a file looking like this: &lt;?php $f = "\x3c\x3f\x70..."; print $f; ?&gt;<br/>Useful for embedding pictures in PHP files.

Sorry folks, comments have been deactivated for now due to the large amount of spam.

Please try to post your questions or problems on a related programming board, a suitable mailing list, a programming chat-room,
or use a QA website like stackoverflow because I'm usually too busy to answer any mails related
to my code snippets. Therefore please just mail me if you found a serious bug... Thank you!


Older comments:

None.