Jump to content

fwrite() a new line??


helloise

Recommended Posts

try that out and see the results.

 

the commented out version would overwrite the file

 

<?php
//read a file
$my_file = "samplefile.txt";
if (file_exists($my_file)) {
$data = file($my_file);
$total = count($data);
echo "<br />Total lines: $total<br />";
foreach ($data as $line) {
$line = trim($line);
echo "$line<br />";
}

//add a new line to end
$write = fopen($my_file, 'a+');
$message = "added a new line here\r\n";
fputs($write, $message);
fclose($write);

/*
//note the w, this will overwrite the entire contents
$write = fopen($my_file, 'w');
$message = "I just added this line and overwrote the file\r\n";
fputs($write, $message);
fclose($write);
*/

} else {
echo "No file to display";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/234317-fwrite-a-new-line/#findComment-1204372
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.