# \[PHP\] Keep writing to text file

**URL:** <https://forum.kirupa.com/t/php-keep-writing-to-text-file/58216>\
**Category:** programming\
**Created:** [July 20, 2004, 7:06pm UTC](https://forum.kirupa.com/t/php-keep-writing-to-text-file/58216 "2004-07-20T19:06:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 20, 2004, 7:06pm UTC](https://forum.kirupa.com/t/php-keep-writing-to-text-file/58216/1 "2004-07-20T19:06:35Z")

</div>

I can write to a textfile, but each time I write to it the old data get’s replaced with the new data.

I need the file to keep all the records, not just the current.

```php

<?php

// request varibales from flash form
$strCombo = $_POST['Combo'];
$strTown = $_POST['Town'];
$strEmail = $_POST['Email'];
$strName = $_POST['Name'];
$strCounty = $_POST['County'];
$strWebsite = $_POST['Website'];
$strCompany = $_POST['Company'];
$strPostcode = $_POST['Postcode'];
$strEnquiry = $_POST['Enquiry'];
$strAddress = $_POST['Address'];
$strTel = $_POST['Tel'];
$strFax = $_POST['Fax'];

// open text file
$fp = fopen("contacts.txt", "w");
if(!$fp){
	echo "Could not open the file. Please try again later. ";
	exit;
}
$inputString = "Name: " . $strName . "
 Email: " . $strEmail;
fwrite( $fp, $inputString );
fclose( $fp );

?>

```

Thanks!!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 20, 2004, 7:18pm UTC](https://forum.kirupa.com/t/php-keep-writing-to-text-file/58216/2 "2004-07-20T19:18:34Z")

</div>

you could do this:

```php

   <?php 
  
 // request varibales from flash form 
 $strCombo = $_POST['Combo']; 
 $strTown = $_POST['Town']; 
 $strEmail = $_POST['Email']; 
 $strName = $_POST['Name']; 
 $strCounty = $_POST['County']; 
 $strWebsite = $_POST['Website']; 
 $strCompany = $_POST['Company']; 
 $strPostcode = $_POST['Postcode']; 
 $strEnquiry = $_POST['Enquiry']; 
 $strAddress = $_POST['Address']; 
 $strTel = $_POST['Tel']; 
 $strFax = $_POST['Fax']; 
  
  $data = file_get_contents("contacts.txt");
 // open text file 
 $fp = fopen("contacts.txt", "w"); 
 if(!$fp){ 
 	echo "Could not open the file. Please try again later. "; 
 	exit; 
 } 
 $inputString = "Name: " . $strName . "
 Email: " . $strEmail; 
  $newData = $data."
".$inputString;
 fwrite( $fp, $newData ); 
 fclose( $fp ); 
 ?>  
  

```

edit: or use “a+” mode for opening the file but I’m not sure abotu that

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 20, 2004, 7:23pm UTC](https://forum.kirupa.com/t/php-keep-writing-to-text-file/58216/3 "2004-07-20T19:23:02Z")

</div>

:thumb:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 23, 2004, 1:14pm UTC](https://forum.kirupa.com/t/php-keep-writing-to-text-file/58216/4 "2004-07-23T13:14:20Z")

</div>

I just did a quick test with a+ and it worked… 🙂

```php

<?php

	// request varibales from flash form
	$strCombo = $_POST['Combo'];
	$strTown = $_POST['Town'];
	$strEmail = $_POST['Email'];
	$strName = $_POST['Name'];
	$strCounty = $_POST['County'];
	$strWebsite = $_POST['Website'];
	$strCompany = $_POST['Company'];
	$strPostcode = $_POST['Postcode'];
	$strEnquiry = $_POST['Enquiry'];
	$strAddress = $_POST['Address'];
	$strTel = $_POST['Tel'];
	$strFax = $_POST['Fax'];

	// open text file
	$fp = fopen("contacts.txt", "a+");
	if (!$fp)
	{
		echo "Could not open the file. Please try again later. ";
		exit;
	}
	$inputString = "Name: " . $strName . "
 Email: " . $strEmail;
	fwrite( $fp, $inputString );
	fclose( $fp );

?>

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 23, 2004, 3:23pm UTC](https://forum.kirupa.com/t/php-keep-writing-to-text-file/58216/5 "2004-07-23T15:23:07Z")

</div>

Thanks 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 23, 2004, 3:26pm UTC](https://forum.kirupa.com/t/php-keep-writing-to-text-file/58216/6 "2004-07-23T15:26:14Z")

</div>

yea, you should use a+… only, it writes the data onto the back of the file…

:p:
