1
私は自分のウェブサイトに「投票」投票を行い、結果を保存するにはテキストファイルを使用します。私は基本的に既存の結果を読んで、新しい結果を増やして再び保存しています。PHPテキストファイルへの書き込みと読み込み
ただし、ファイルを読み取り、ファイルを保存しているようです。しかし、後でチェックするためにデータを再読み込みすると、ファイルが正しく保存されていないように見えます...私は何が起きているのかわかりません。テキストファイル。テキストファイル形式で保存され
<?php
$vote = $_REQUEST['vote'];
//open file read current votes
$contents = file("poll_result.txt");
//put content in array, split between the ;
$array = explode(";", $contents[0]);
$yes = $array[0];
$no = $array[1];
echo("Opened file and read contents. YES-" . $yes . " NO-" . $no . "<br>");
//Check if it's a yes or no vote
if ($vote == 0)
{
$yes = $yes + 1;
echo("Incremented yes vote, it is now" . $yes . "<br>");
}
if ($vote == 1)
{
$no = $no + 1;
echo("Incremented no vote, it is now" . $no . "<br>");
}
//insert new votes to txt file
$insertvote = $yes. ";". $no;
echo("To insert: " . $insertvote . "<br>");
$wfile = fopen('poll_result.txt', w);
fputs($wfile, $insertvote);
echo("Done.");
//////////////////////////////////////////////////
//open file read current votes
$contents = file("poll_result.txt");
//put content in array, split between the ||
$array = explode(";", $contents[0]);
$yes = $array[0];
$no = $array[1];
echo("Re-read data: " . $yes . "|" . $no);
?>
:ここ
は、コードpoll_vote.phpで 0;あなたが適切にディスクに書き込むファイルを閉じる必要があります0
エラー報告をオンにします。報告されているエラーはありますか?ファイルを検査します。最終更新日は更新されていますか?ヒント:変数にファイル名を入れて、everyplaceの "poll_results.txt"をハードコーディングするのではなく、それを使用してください.... –
また、このポーリングがどのくらいビジーであるかに応じて、ファイルが既に開いている場合は、書き込みのために。データが失われる可能性があります。 –
'w 'に引用符がありません。あなたはファイルを閉じていません。 –