I am trying to write to some txt files using SplFileObject's fwrite method and getting below error message.
Fatal error: Maximum execution time of 30 seconds exceeded in /Users/oliverwilliams/Desktop/olliephp/directory.php on line 9.
Line 9 is while (!$textfile->eof()) {
from the following code
$dir = new FilesystemIterator('garbagedirectory');
foreach ($dir as $file) {
if ($file->getExtension() === 'txt') {
$textfile = $file -> openFile('r+');
while (!$textfile->eof()) {
$textfile -> next();
}
$textfile->fwrite("This line was added dynamically");
}
}
I am new to PHP. How get rid of that error?
This is the code I was emulating, from David Powers Lynda course. His code does work.
$docs = new FilesystemIterator('garbagedirectory');
foreach ($docs as $doc) {
if ($doc->getExtension() == 'txt') {
$textfile = $doc->openFile('r+');
$textfile->setFlags(SplFileObject::SKIP_EMPTY | SplFileObject::READ_AHEAD
| SplFileObject::DROP_NEW_LINE);
echo '<h2>' . $textfile->getFilename() . '</h2>';
foreach ($textfile as $line) {
echo "$line<br>";
}
$textfile->seek(3);
echo '<br>';
echo 'This is the fourth line: ' . $textfile->current();
while(!$textfile->eof()) {
$textfile->next();
}
$textfile->fwrite("\r\n\r\nThis line was added dynamically");
}
}