-2
$testarray['player1'] = $player1Plays;
$testarray['player2'] = $player2Plays;
$testarray['result'] = $result;

print_r ($testarray);

$yoyo = serialize ($testarray);

$file = 'prevdata.dat';
fopen ($file, 'w');
file_put_contents($file, trim($yoyo) . PHP_EOL, FILE_APPEND);

I'm making a small rock, paper, scissors game for class and need to save each move and results to a file. This is what I have so far and it works to serialize the data and save it to the file, but every time I play the game again it writes over the data currently in the file (I thought 'FILE_APPEND' was supposed to add on). The full code is provided here https://eval.in/624620

4

2 回答 2

1

改变

$file = 'prevdata.dat';
fopen ($file, 'w');
file_put_contents($file, trim($yoyo) . PHP_EOL, FILE_APPEND);

要么

$fp = fopen('prevdata.dat', 'a'); fwrite($fp, trim($yoyo));

或者

file_put_contents('prevdata.dat', trim($yoyo), FILE_APPEND);
于 2016-08-18T01:41:28.817 回答
0

使用正确的fopen函数模式很重要。您需要将指针(您可以将其视为写入头)放在文件末尾,如下所示:

fopen($file, 'a');

查看文档以查看所有可能的模式。

http://php.net/manual/en/function.fopen.php

于 2016-08-18T01:39:59.517 回答