# Reverse lines in textfile

**URL:** <https://forum.kirupa.com/t/reverse-lines-in-textfile/275970>\
**Category:** programming\
**Created:** [November 20, 2008, 7:51pm UTC](https://forum.kirupa.com/t/reverse-lines-in-textfile/275970 "2008-11-20T19:51:07Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![driz](https://avatars.discourse-cdn.com/v4/letter/d/e5b9ba/32.png) [@driz](https://forum.kirupa.com/u/driz)\
**Post date:** [November 20, 2008, 7:51pm UTC](https://forum.kirupa.com/t/reverse-lines-in-textfile/275970/1 "2008-11-20T19:51:07Z")

</div>

Hi, I’m trying to reverse the lines in my text file, here is my code:

```auto
	$filename = "comments.txt";
		$date = date('jS F Y');
		$time = date('h:i a');
		$name = $_POST['name'];
		$email = $_POST['email'];
		$website = $_POST['website'];
		$message = $_POST['message'];
		$submit = $_POST['submit'];
		
		// when user submits the form
		if($submit=="submit") {
		
			// open text file
			$fp=fopen($filename, 'a+');
			// then using this format
			$fstring="||" . $date . "||" . $time . "||" . $name . "||" . $email . "||" . $website . "||" . $message . "
\r";
			// write the comment
			fwrite($fp, $fstring);
			// close text file
			fclose($fp);
			
		};

```

This is what I have come up with:

```auto
fputs ($fp, array_reverse ($fstring));

```

I have added it here:

```auto
		// open text file
			$fp=fopen($filename, 'a+');
			// then using this format
			$fstring="||" . $date . "||" . $time . "||" . $name . "||" . $email . "||" . $website . "||" . $message . "
\r";
			// write the comment
			fwrite($fp, $fstring);
			// reverse the comments
			fputs ($fp, array_reverse ($fstring));
			// close text file
			fclose($fp);

```

The comments don’t reverse though! They still show the original way?

Thanks. x
