Working with files and getting an error - worked earlier!

Hello! I’m working with files and this program worked on a laptop. I opened it on my desktop and now it won’t run. Getting this error message: “Maximum execution time of 30 seconds exceeded on line 19”

Line 19 is this portion:

$fp = fopen(“guestBook.txt”, “r”);
$guestInfo = “”;
while (!feof($fp)){
$guestInfo = fgets($fp);
}
fclose($fp);

[php]

Guest Book <?php //retrieve data from form $gName = filter_input(INPUT_POST, "gName"); $comments = filter_input(INPUT_POST, "comments");

$fp = fopen(“guestBook.txt”, “a”);
fputs($fp, "Name: " .$gName. ", Comments: ". $comments);
fclose($fp);

$fp = fopen(“guestBook.txt”, “r”);
$guestInfo = “”;
while (!feof($fp)){
$guestInfo = fgets($fp);
}
fclose($fp);

$fp = fopen(“guestBook.txt”, “a”);
fputs($fp, “\n”);
fclose($fp);

?>

Guest Book

Name:

Comments:


Submit

<?php echo $guestInfo; ?>

[/php]

How large is the guestBook.txt file?

Are you really only after the last line? If so you can read the file in reverse and just stop after 1 iteration instead of looping through every line of the file…

The guestBook.txt file is empty. Yes I will only be after the last line and when it was working it was just printing the last line. I’m new at this so what do you mean “If so you can read the file in reverse and just stop after 1 iteration instead of looping through every line of the file…”

Also, why would this program work perfectly and then stop working? It does not open the guestBook.php program at all now, just gives the error message.

I got it, thank you!!

Sponsor our Newsletter | Privacy Policy | Terms of Service