2016-10-04 30 views
0

如何保存多个数据字符串而不删除旧的(在同一个文件中)?如何保存多个数据字符串而不删除旧的(在同一个文件中)?

我使用POST从形式到TXT文件添加数据,但它一直覆盖旧数据:

$txt = "joined_teams.txt"; 
$fh = fopen($txt, 'w+'); 

if (isset($_POST['teamname']) && isset($_POST['shortname']) && isset($_POST['1steamid']) && isset($_POST['2steamid']) && isset($_POST['3steamid']) && isset($_POST['4steamid']) && isset($_POST['5steamid'])){ // check if both fields are set 
    $txt=$_POST['teamname'].' - '.$_POST['shortname'].' - '.$_POST['1steamid'].' - '.$_POST['2steamid'].' - '.$_POST['3steamid'].' - '.$_POST['4steamid'].' - '.$_POST['5steamid']; 
    file_put_contents('joined_teams.txt',$txt."\n",FILE_APPEND); // log to joined_teams.txt 
    exit(header('location:http://coprogames.com')); 
} 

fwrite($fh,$txt); // Write information to the file 
fclose($fh); // Close the file 

回答

0

你两次做同样的事情。你只需要拨打file_put_contents并且可以删除fopen/fwrite调用。所有你需要的是:

if (isset($_POST['teamname']) && isset($_POST['shortname']) && isset($_POST['1steamid']) && isset($_POST['2steamid']) && isset($_POST['3steamid']) && isset($_POST['4steamid']) && isset($_POST['5steamid'])){ // check if both fields are set 
    $txt=$_POST['teamname'].' - '.$_POST['shortname'].' - '.$_POST['1steamid'].' - '.$_POST['2steamid'].' - '.$_POST['3steamid'].' - '.$_POST['4steamid'].' - '.$_POST['5steamid']; 
    file_put_contents('joined_teams.txt',$txt."\n",FILE_APPEND); // log to joined_teams.txt 
    exit(header('location:http://coprogames.com')); 
} 
0

你也可以尝试这样的事情。

file_put_contents('joined_teams.txt', $txt.PHP_EOL , FILE_APPEND | LOCK_EX); 
相关问题