1

SplFileObject の fwrite メソッドを使用していくつかの txt ファイルに書き込もうとしていますが、以下のエラー メッセージが表示されます。

致命的なエラー: 9 行目の /Users/oliverwilliams/Desktop/olliephp/directory.php で最大実行時間が 30 秒を超えました。

9行目はwhile (!$textfile->eof()) {次のコードからのものです

$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");
    }
}

私はPHPが初めてです。どのようにそのエラーを取り除くのですか?

これは、David Powers Lynda コースから、私がエミュレートしていたコードです。彼のコードは機能します。

$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");
    }
    }
4

0 に答える 0