2

我有这个代码:

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

我发现了这个错误:Warning: fwrite(): 120 is not a valid stream resource

有什么想法我能做什么?

伙计们:必须添加正确书写的第一行。(!!!)

4

2 回答 2

2

测试打开文件进行写入的权限

$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."\r\n";
    fwrite($file, $text);
    fclose($file);
    sleep(5);
}
于 2012-08-16T13:01:04.470 回答
0

如果代码 beetween ... 和 $this->realIdTraining 存在于某处,这应该可以工作

此外,如果需要,首先验证文件是否存在,以及其他内容,如写入权限。

<?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."\r\n";
fwrite($file, $text);
fclose($file);
sleep(5);

?>
于 2012-08-16T12:59:03.893 回答