PHP/File Directory/File Write
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>.");
?>
Writing and Appending Content in a File
<?php
$myfile = "./test.txt";
$openfile = fopen ($myfile,"w") or die ("Couldn"t open the file");
fwrite ($openfile,"This is a string \n");
fclose ($openfile);
$openfile = fopen ($myfile,"r") or die ("Couldn"t open the file");
$file_size=filesize($myfile);
$file_contents = fread ($openfile,$file_size);
$msg ="$file_contents";
fclose ($openfile);
echo $msg;
?>