Sunday, January 31, 2016

Write Data to a File using PHP Functions

A method to write data to a file saved in the local machine using PHP functions.

<?php
//write to file | a-append w-overwrite
function writeToFile($filename,$datatosave,$mode)
{
if (!file_exists($filename))
{
echo "file not found";
echo "<br/>";
}
else
{
$fh = fopen($filename,$mode) or die("Can't open file");
fwrite($fh,$datatosave);
fclose($fh);
echo "File written successfully.";
}
return;
}

echo "<br/>";
writeToFile("test.txt","$today\n","a");
echo "<br/>";

No comments:

Post a Comment