PHP数组写入错误

PHP数组写入错误,php,Php,我正在尝试为一个网站编写一个非常简单的php投票系统。基本上,选票统计在一个有3行的文本文件中。每一行只是一个数字。没有任何投票的投票文件如下所示: 0 0 0 以下是我正在使用的php: $file = "votes.txt"; $votes = file($file); $votes[0] = intval($votes[0]) + 1; $voteWrite = strval($votes[0]) . "\n" . strval($votes[1]) . "\n" . strval($

我正在尝试为一个网站编写一个非常简单的php投票系统。基本上,选票统计在一个有3行的文本文件中。每一行只是一个数字。没有任何投票的投票文件如下所示:

0
0
0
以下是我正在使用的php:

$file = "votes.txt";
$votes = file($file); 
$votes[0] = intval($votes[0]) + 1;
$voteWrite = strval($votes[0]) . "\n" . strval($votes[1]) . "\n" . strval($votes[2]);
file_put_contents($file, $voteWrite);
因此,数组保存文本文件的每一行,如果这是投票给第一个选项的代码,则该行的值增加1,然后将整个数组连接成一个字符串后写回文件,保留数组的行。理想情况下,文本文件现在应为:

1
0
0
但实际上它是这样写的:

10
0
有人能告诉我这是为什么吗?php真的不想和我一起工作。。。 谢谢

编辑:

我真的不懂这个file()的东西…我设置了一个测试:

$file = "votes.txt";
$votes = file($file); 
$option1 = $votes[0];
$option2 = $votes[1];
$option3 = $votes[2];
然后,在JavaScript中:

alert(<?php echo $option1; ?>);
alert(<?php echo $option2; ?>);
alert(<?php echo $option3; ?>);
alert();
警惕();
警惕();

文本文件在3行上分别读取28、3和49。警报返回“28”、“450”和“未定义”。怎么回事?

像这样的东西可能会有帮助:

// Define the whole path of where the file is
$fileName = '/var/home/votes/votes.txt';

// Check if the file exists - if not create it
if (!file_exists($fileName))
{
    $data = array('0', '0', '0');
    $line = implode("\n", $data);
    file_put_contents($line, $fileName);
}

// The file exists so read it
$votes = file($fileName);

// What happens if the file does not return 3 lines?
$vote_one   = isset($votes[0]) ? intval($votes[0]) : 0;
$vote_two   = isset($votes[1]) ? intval($votes[1]) : 0;
$vote_three = isset($votes[2]) ? intval($votes[2]) : 0;

$vote_one++;

$line = "{$vote_one}\n{$vote_two}\n{$vote_three}";
file_put_contents($line, $fileName);
除了删除stru val并进行一些错误检查之外,我没有做什么。试试看,也许效果会更好

如果选择不同的存储方法,可能会更好。稍微改变一下格式将为将来提供更多的灵活性,但这取决于您自己,取决于您的约束条件

使用类似于和的东西,将允许您在文件中存储甚至结构化的数据,并像以前一样检索它,而且它是UTF8安全的

因此,如果您选择使用该路线,您的代码将变成:

// Define the whole path of where the file is
$fileName = '/var/home/votes/votes.txt';

// Check if the file exists - if not create it
if (!file_exists($fileName))
{
    $data = array('0', '0', '0');
    $line = json_encode($data);
    file_put_contents($line, $fileName);
}

// The file exists so read it
$line = file_get_contents($fileName);
$data = json_decode($line, TRUE);

$data[0] = intval($data[0]) + 1;

$line = json_encode($data);
file_put_contents($line, $fileName);
json_decode需要TRUE作为第二个参数,以便在解码数据时创建关联数组


HTH

有什么原因不能将信息存储为序列化数组?如果不是,您可能需要考虑这个解决方案:

$file = "votes.txt";
$votes = file_get_contents($file); 
$votes = $votes ? unserialize($votes) : array(0,0,0);

$votes[0] += 1;

file_put_contents($file, serialize($votes));
如果您正在以其他语言或出于其他目的重用此文件,我认为JSON建议是一个很好的建议,甚至可以像CSV一样简单地使用逗号分隔来存储值

至于为什么你的例子不起作用:我不完全确定。零和行尾的解释有时会很奇怪。我想知道零值是否会在某个地方抛出PHP,可能会将一行解释为空?您可能希望尝试一个如下所示的起始
投票.txt
文件:

1     
1
1

看看它是否仍然是这样的。

好吧,奇怪的数字问题是因为没有在数组值周围使用intval(),而是使用@file\u put\u contents而不是file\u put\u contents…但是我不能走这条路,我想得不够远,PHP将无法满足我的需求。

为什么不使用数据库?在您的
文件()显示后,
var\u转储($vots)
什么?你的文件被正确读取了吗?另外,你在那里有一个基本的竞争条件,并且在某个时候会失去选票。该网站是通过大学服务器托管的,与大学合作在网站上托管数据库是一场噩梦。使用序列化/非序列化(甚至是json_encode/json_decode)而不是直线定位可能更直观的数据storage@MarcB假设OP无法获得任何类型的数据库服务,您会建议如何处理竞争条件?使用append将每个投票写入一个文件会更安全吗?