Reputation: 57
Hello guys the following code will add only one line of text to my 'file.txt' whenever i m
going to add more text so its replacing the old one, i want to have the old one with new also
Beside this one i want to separate the words in my 'txt.file' by a comma and space.
Here is my code:
<?php
$Fname = $_POST['fname'];
$email = $_POST['email'];
$Phone = $_POST['number'];
$date = $_POST['date'];
$my_file = 'file.txt';
$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);
fwrite($handle, $Fname);
fwrite($handle, $email);
fwrite($handle, $Phone);
fwrite($handle, $date);
?>
Upvotes: 0
Views: 60
Reputation: 932
//or you can use file_put_content to add to end of each line
$my_file = 'file.txt';
file_put_content($my_file, $Fname, PHP_EOL, PHP_APPEND);
file_put_content($my_file, $emai, PHP_EOL, PHP_APPEND);
file_put_content($my_file, $phone, PHP_EOL, PHP_APPEND);
Upvotes: 0
Reputation: 2912
Because you are using write mode (w
). To add lines to the end, you have to append (a
):
$handle = fopen($my_file, 'a');
To separate words by comma and space, join it into one string in PHP and than save it:
$finalString = $Fname . ', ' . $email . ', ' . $Phone . ', ' . $date;
fwrite($handle, $finalString );
Upvotes: 3