<?php

function file_reverse()
{
	$datafile = "test.txt";
	$outfile = "test2.txt";

	if(!file_exists($datafile)) 
	{
	  trigger_error("Couldn't find datafile $datafile.", E_USER_ERROR);
	}
	
	$fd = fopen($datafile, "rt");
	$lines = array();
	while (!feof ($fd)) 
	{ 
	  array_push($lines, fgets($fd, 4096)); 
	}
	fclose($fd);

	$fd = fopen($outfile, "wt");
	foreach (array_reverse($lines) as $line) fputs($fd, $line);
}

for ($i = 0; $i < 10; $i++) file_reverse();
?>