Jump to content

How to write array into csv last line without overwritten the whole csv


Recommended Posts

I have any array with name, gender , age

 

I want to write into a csv which already contains many records line by line,

 

I would like to write the array elements into the csv as a line line from the bottom without overwritten the whole csv.

 

How to do so?

 

Thanks

Here's code to show and write last line to a text file, the commented one is to replace the entire files contents.

 

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

//add a new line to end
$write = fopen($my_file, 'a+');
$message = "I just added this line\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\r\n";
fputs($write, $message);
fclose($write);
*/

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.