Php写入文件

I have this code:

$file = fopen($_SERVER['DOCUMENT_ROOT'].'crawl.txt', 'w+');
$time1 = microtime(true);
......
$time2 = microtime(true);
$time = $time2-$time1;
$text = "Training id: ".$this->realIdTraining." Time: ".$time."
";
fwrite($file, $text);
fclose($file);
sleep(5);

I catch this error: Warning: fwrite(): 120 is not a valid stream resource

Any ideas what can I do?

Guys: Have to add that first row written correctly.(!!!)

This should work, if the code beetween ... and $this->realIdTraining exists somewhere

Also, verify first if the file exists , and others, like write permissions, if needed.

<?php

$file = fopen($_SERVER['DOCUMENT_ROOT'].'/crawl.txt', 'w+');
$time1 = microtime(true);
...
$time2 = microtime(true);
$time = $time2-$time1;
$text = "Training id: ".($this->realIdTraining)." Time: ".$time."
";
fwrite($file, $text);
fclose($file);
sleep(5);

?>

test for permission to open the file for writing

$file = fopen($_SERVER['DOCUMENT_ROOT'].'/crawl.txt', 'w+');
if(!$file)
{
     echo 'cannot write to file';
}
else
{
    $time1 = microtime(true);
    ...
    $time2 = microtime(true);
    $time = $time2-$time1;
    $text = "Training id: ".$this->realIdTraining." Time: ".$time."
";
    fwrite($file, $text);
    fclose($file);
    sleep(5);
}