This question has already been answered:
I use the following code for reviews on the site, they are written to the end of the file, the new review is at the bottom of the file, and at the top of the file is the old review. How to write to the top of the file a new review? Code:
< ? if ($_GET['c'] == '') { print " <form> <input type='hidden' name='c' value='obr' /> <input type='text' id='reviews_form_name' name='name' placeholder='Имя' value='' /><br /> <textarea name='content' id='reviews_form_text' placeholder='Отзыв...'></textarea><br /> <input type='submit' id='button_form_reviews' value='Оставить свой отзыв' /> </form> "; $fp = fopen("comment.txt", "r"); // Открываем файл в режиме чтения if ($fp) { while (!feof($fp)) { $mytext = fgets($fp, 999); echo $mytext . "<br />"; } } else echo "Ошибка при открытии файла"; fclose($fp); } elseif ($_GET['c'] == 'obr') { // заносим в массив значение полей $znach = array( 1 => $_GET['name'], 3 => $_GET['content'] ); if (!$znach[1]) { print "Поле <b>Имя</b>, незаполненно <br /> <meta http-equiv='Refresh' content='4; url=javascript:history.go(-1);' ><a href='javascript:history.go(-1);'><<<Назад</a> <br />"; } else if (!$znach[3]) { print "Поле <b>Отзыв</b>, незаполненно <br /> <meta http-equiv='Refresh' content='4; url=javascript:history.go(-1);' ><a href='javascript:history.go(-1);'><<<Назад</a> <br />"; } else { $fp = fopen("comment.txt", "a+"); // Открываем файл в режиме записи $mytext = "\n\n\r\n" . "Дата: " . date('Y:m:d') . "\r\n" . "Имя: " . $znach[1] . "\r\n" . "Отзыв: " . "\r\n" . $znach[3] . "\r\n"; $test = fwrite($fp, $mytext); // Запись в файл if ($test) echo 'Данные в файл успешно занесены.'; else echo 'Ошибка при записи в файл.'; fclose($fp); //Закрытие файла print "<meta http-equiv='Refresh' content='0; url=?c=' >"; } } ?>