php fwrite不写

时间:2014-06-18 03:41:33

标签: php file fwrite

我正在尝试将teamspeak的昵称写入文件。

它经历了所有检查,但是当我之后检查文件时,没有任何内容被写入。

$file = "test.txt";

$fh = fopen($file, 'w');

if(file_exists($file))
{
    echo "exist";   
}else{
    echo "absent";
}

if(is_writable($file))
{
    echo "writable";
}else {
    echo "cant write";
}

if (false === $fh) {
    throw new RuntimeException('Unable to open log file for writing');
}

foreach($Summoners as $su){
 $name = $su["TsNICK"];
 echo $name;

 fwrite($fh, $name);

 echo "Wrote to file";


}
fclose($fh);

1 个答案:

答案 0 :(得分:0)

我无法确定你的代码无法正常工作,但你应该考虑将其重写为:

$file = "test.txt";

if (false === ($fh = fopen($file, 'w'))) {
    throw new RuntimeException('Unable to open log file for writing');
}

foreach($Summoners as $su) {
    $name = $su["TsNICK"];
    echo $name;
    fwrite($fh, "$name\n");
}

fclose($fh);

如果您希望始终附加到文件,则需要'a'模式而不是'w'