";
// write content to a file (write mode)
file_put_contents("output.txt", $content);
// open a file in a write mode
$outfile = fopen("output-write.txt", "w");
fputs($outfile, $content); // content must be string
fclose($outfile);
// open a file in an append mode
$outfile = fopen("output-append.txt", "a");
fputs($outfile, $content); // content must be string
fclose($outfile);
// open a fle in a read mode
$infile = fopen("sample-data.txt", "r");
while (!feof($infile))
{
$content = fgets($infile); // get one line at a time
echo $content . "
";
}
// open a file, read the content in,
// separated by a new line character, return an array of lines
$array_of_lines = file("sample-data.txt");
foreach ($array_of_lines as $line)
echo "$line
";
// suppose we want to write array data to file
// separate each array item to each line
$days = ["Monday", 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];
file_put_contents("output-array.txt", $days);
$formatted_days = implode("\n", $days); // note: double quote
file_put_contents("output-array.txt", $formatted_days);
$associative_days = Array("Mon"=>"Monday", "Tue"=>"Tuesday", "Wed"=>"Wednesday",
"Thur"=>"Thursday", "Fri"=>"Friday");
// var_dump($days);
var_dump(implode("@", $associative_days)); // implode "@" and value, not key
$content = "";
foreach ($associative_days as $k => $v)
{
$content .= "$k => $v \n";
}
file_put_contents("output-array.txt", $content);
?>