fwrite方法超出最大执行时间

时间:2016-03-05 12:35:32

标签: php fwrite spl splfileobject

我正在尝试使用SplFileObject的fwrite方法写入一些txt文件并获取以下错误消息。

  

致命错误:超过30秒的最长执行时间   第9行/Users/oliverwilliams/Desktop/olliephp/directory.php。

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

0 个答案:

没有答案