login

How to Read Data from a Binary File

<?php
//First, dictate a file.
$binary_file = "data.dat";
//Now, we use the file_exists() function to confirm its existence.
if (file_exists ($binary_file)){
    //Then open the file for binary read and writing.
    try {
        if ($read_file = fopen ($binary_file, "rb+")){
            //Now, we can read and write in binary.
            $curtext = fread ($read_file,filesize($binary_file));
            echo $curtext;

            //Then we close the file.
            fclose ($read_file);
        }
    } catch (exception $e) {
        echo $e->getmessage();
    }
} else {
    echo "Sorry, file does not exist.";
}
?>