Search code examples
phphexfopenbin

reading a bin file with php bin2hex function


I am trying to read a bin file that contains a lots of two 4-byte numbers in it, which I want to read and convert to hex numbers that is then going to be printed to the screen.... hopefully however I am having a little trouble getting my head around this one. this is what I have so far from reading examples and documentation..

<?php

$handle = @fopen("files/bigbin1.bin", "r");
if ($handle) {
    while (!feof($handle)) {
        $hex = bin2hex($handle);
    }
    fclose($handle);

}

print_r($hex);
?>

I am 95% sure the error is in passing $handle over to tbin2hex.. but this being my first ever reading of a bin file I am slightly lost. the overall goal at some point will be to read the bin file into the database however I am just trying to figure out what this file looks like on screen.


Solution

  • <?php
    
    $handle = @fopen("files/bigbin1.bin", "r");
    if ($handle) {
        while (!feof($handle)) {
            $hex = bin2hex(fread ($handle , 4 ));
            print $hex."\n";
        }
        fclose($handle);
    
    }
    
    ?>
    

    EDIT: Also you should avoid using @ it can make debugging extremely frustrating.