2016-04-30 44 views
1

我有这段代码打开文件夹中的每个文件,将值减少5,然后覆盖现有的内容。添加新行的字符串,我不明白为什么

<?php 
    foreach (glob("users/*.php") as $filename) {  
     $array = file($filename); 

     $wallet = $array[0]; 
     $time = $array[1]; 
     $status = $array[2]; 
     $steamid = $array[3]; 

    $process = fopen($filename, "w") or die("Unable to open file!"); 
     $time = $time - 5; 
     $newdata = $wallet . "\n" . $time . "\n" . $status . "\n" . $steamid; 
     fwrite($process, $newdata); 
    fclose($process); 
} 
?> 

之前,我执行脚本,被打开的文件是这样的:

680 
310 
0 
74892748232 

脚本执行完毕后,文件看起来是这样的:

680 

305 
0 

74892748232 

如果脚本再次执行,它会添加更多行,并且更多地破坏文件。

该文件的字符串在这里创建:

$newdata = $wallet . "\n" . $time . "\n" . $status . "\n" . $steamid; 

为什么它添加$钱包和$状态后一个空行,但不经过$时间?我怎样才能避免这种情况,而是写下:

$wallet 
$time 
$status 
$steamid 

感谢很多提前:)

+0

Depe发现你的操作系统如何对待新线路。尝试使用'$钱包。 PHP_EOL。 $ time'。 –

+0

使用功能文件FILE_IGNORE_NEW_LINES 和 设置这两个标记非常有用FILE_SKIP_EMPTY_LINES – splash58

回答

相关问题