login

Writing Binary Data to a 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+")){
            //Then we can write to it.
            fwrite ($read_file, "Writing binary to file");

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