Write content to file : File Write « File Directory « PHP






Write content to file

<?php 

$cfile = "test.txt";

$fh = @fopen($cfile, "r+") or die("<BR>Failed to open file <I>$cfile</I>.");
@flock($fh, LOCK_EX)  or die("<BR>Could not lock file <I>$cfile</I>.");

$s = @fgets($fh, 6);
$count = (int) $s + 1;
$count = str_pad($count, 6);

@rewind($fh)  or die("<BR>Failed to rewind file <I>$cfile</I>.");

if (@fwrite($fh, $count) == -1) {
    die("<BR>Failed to write to file <I>$cfile</I>.");
}

echo "$count";
@flock($fh, LOCK_UN) or die("<BR>Could not unlock file <I>$cfile</I>.");
fclose($fh) or die("<BR>Failed to close file <I>$cfile</I>.");
?>



           
       








Related examples in the same category

1.Writing and Appending Content in a File
2.Writing and Appending to a File