我对php真的很陌生。我决定根据我看过的剧本制作一个计数器。我已经对它做了更改。我正在想办法重置计数器。
$userCount = file_get_contents("count.txt");
$userCount = trim($userCount);
$userCount = $userCount + 1;
$countReset = $userCount - $userCount;
$file = fopen("count.txt","w+");
fwrite($file,$userCount);
fclose($file);
print "The number of visitors is: $userCount";
if ($userCount < 20){
echo 'not yet';
}
else {
echo 'done!';
}
if ($userCount > 40){
fwrite($file,$countReset);
fclose($file);
}
我试着把计数器本身减去,使它回到0。
$countReset = $userCount - $userCount;
然而,它似乎不起作用。计数器本身工作,但我无法将其重置回0。
这只是我正在做的一个类似印象的脚本,作为学习php的一种方式。此外,很抱歉的格式,与这个帖子编辑斗争。
剧本有什么帮助吗?
在再次尝试写入文件之前,您已经关闭了该文件。在第二个fwrite
之前,添加:
$file = fopen("count.txt","w+");
尝试简单地将值0设置为var:
$countReset = 0;
您不能直接编辑count.txt文件吗?
用PHP做,你可以做
fwrite($file,'0');
编辑:就像CanSpice说的,你不应该在处理完文件之前关闭它。删除第一个fclose,它应该会起作用。
在这种情况下,我不会混合使用fopen()
和file_get_content()
函数,要么使用fopen()
、fread()
和fwrite()
,要么使用file_get_contents()
和file_put_contents()
。
如果您只需要重置计数器,而不需要以前的值,则使用:
file_put_contents('count.txt', '0');
如果您需要更新值,您可以使用其中一种:
$count = file_get_contents( 'count.txt');
$count++;
// Reset?
if( $reset){
$count = 0;
}
file_put_contents( 'count.txt', "$count");
或者更确切地说:
$fp = fopen( 'count.txt', 'r+') or die( 'Cannot use counter');
$count = trim( fread( $fp, 1024));
$count++;
// Reset?
if( $reset){
$count = 0;
}
ftruncate( $fp, 0);
fseek( 0, SEEK_SET)
fwrite( $fp, "$count");
fclose( $fp);
以下是ftruncate()
和fseek()
+的手册页,您可能应该研究flock()
,这样两个脚本就不会同时覆盖内容。
/****************************************************************************
* read the current count from the counter file, increment
* it by 1, and re-save the new value off to the file
***************************************************************************/
function getAndIncrementCount(){
// attempt to open the file
// a+ means keep the contents so we can read it at least once,
// but allow us to overwrite the value once we increment it
if (($fHandle = fopen('count.txt','a+')) !== FALSE){
// read in the count (also cast to an int so if there is
// an invalid (or absent) value it will default to a 0
$count = (int) fread($fHandle, 100);
// increase the counter
$count++;
// go back to the beginning of the file
fseek($fHandle, 0);
// re-write the new count back to the file
fwrite($fHandle, $count);
// cose the file now that we're done with it
fclose($fHandle);
// return back the count
return $count;
}
// we couldn't get to the file, so return an error flag
return FALSE;
}
/****************************************************************************
* write the specified value to the counter file
***************************************************************************/
function setCount($count = 0){
// attempt to open the file with over-write permissions
// w+ will open the file and clear it
if (($fHandle = fopen('count.txt','w+')){
// write the counter to the file
fwrite($fHandle, $count);
// close the file now that we're done
fclose($fHandle);
// return the newly saved count
return $count;
}
// we couldn't get to the file, so return an error flag
return FALSE;
}
并在实践中应用:
$userCount = getAndIncrementCount();
echo "The number of visitors is: {$userCount}";
if ($userCount < 20){
echo "Not Yet";
}else{
echo "Done!";
}
if ($userCount > 40){
setCount(0);
}
这是因为您不是在重写文件内容,而是在第二次使用fwrite时添加到文件内容中,所以$countReset会附加到文件中已有的内容中。试试这个:
$userCount = file_get_contents("count.txt");
$userCount = $userCount + 1;
$countReset = $userCount - $userCount;
file_put_contents("count.txt", $userCount);
print "The number of visitors is: $userCount'n";
if ($userCount < 20){
echo 'not yet';
}
else {
echo 'done!';
}
if ($userCount > 40){
file_put_contents("count.txt", $countReset);
}