Reading and Writing Binary Data in a File : fread « File Directory « PHP






Reading and Writing Binary Data in a File

 
<?php 
$binfile = "data.txt"; 
if (file_exists ($binfile)){ 
    try { 
        if ($readfile = fopen ($binfile, "rb+")){ 
            $curtext = fread ($readfile,100); 
            echo $curtext; //Hello World! 
            fwrite ($readfile, "Hi World!"); 
            fclose ($readfile); 
        } 
    } catch (exception $e) { 
        echo $e->getmessage(); 
    } 
} else { 
    echo "Sorry, file does not exist."; 
} 
?>
  
  








Related examples in the same category

1.Reading a file
2.fread( ) is good for when you only care about a small part of the file.
3.fread() function reads up to length bytes from the file, returning the file's contents.
4.fread.php
5.fopen( ) and fread( )