Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的fopen()PHP有什么问题_Php_Fopen - Fatal编程技术网

我的fopen()PHP有什么问题

我的fopen()PHP有什么问题,php,fopen,Php,Fopen,我有一个要保存的变量,它是: $counter = $counter['counter']++; 使其在刷新页面时递增。 因此,我决定使用fopen,重写$counter变量,然后保存它。 我还是一个初学者,我不知道fopen、fgets和fclose是如何工作的 所以我写了这样的东西 $fp = fopen("file.php","r"); fwrite($fp, $counter); fclose($fp); 因此,我想打开文件(file.php)(我正在编写代码的文件,仅供参考),然后

我有一个要保存的变量,它是:

$counter = $counter['counter']++;
使其在刷新页面时递增。 因此,我决定使用fopen,重写$counter变量,然后保存它。 我还是一个初学者,我不知道fopen、fgets和fclose是如何工作的

所以我写了这样的东西

$fp = fopen("file.php","r");
fwrite($fp, $counter);
fclose($fp);
因此,我想打开文件(file.php)(我正在编写代码的文件,仅供参考),然后在变量递增后覆盖它,然后通过关闭来保存它

但是代码似乎不想工作,变量似乎也不想递增。 我做错了什么? 我想把这个$counter放在另一个文件中并从那里提取变量吗

仅供参考:我不想使用session\u start()和$\u session,因为我使用的是cron作业,它不会工作

编辑

$result = mysql_query('SELECT MIN(ID) AS min, MAX(ID) AS max FROM ytable') or exit(mysql_error()); 
$row = mysql_fetch_assoc($result); 

if($counter['counter'] < $row['max']){
    if (isset($counter['counter'])){
        $counter = $counter['counter']++;

    }else{
            $counter = $counter['counter'] = 0;
        }
 }
$result=mysql\u query('selectmin(ID)为MIN,MAX(ID)为ytable中的MAX')或exit(mysql\u error());
$row=mysql\u fetch\u assoc($result);
如果($counter['counter']<$row['max'])){
如果(设置($counter['counter'])){
$counter=$counter['counter']+%;
}否则{
$counter=$counter['counter']=0;
}
}

这是为那些感到困惑的人编写的更多代码

您可以这样阅读文件。用前面的计数器打开文件&取出计数器,像这样递增

$counter = readfile("file.php");
$counter++;
$fp = fopen("file.php","w");
fwrite($fp, $counter);
fclose($fp);
要写入文件,请按如下方式以写入模式打开文件

$counter = readfile("file.php");
$counter++;
$fp = fopen("file.php","w");
fwrite($fp, $counter);
fclose($fp);
这就是你想要的结果吗


正如chris85所建议的,数据库用于此类案例。最好使用数据库而不是文件处理。

因此,假设您有
file.txt
,其中包含
5
。在同一个目录中有
script.php
;这个文件应该是

$counter = file_get_contents('file.txt'); // this takes in whatever value is in the file e.g in this case '5'
$counter++; // increment the value by 1 so we are now at 6
file_put_contents('file.txt', $counter); //write the value '6' back to the file

file.txt
在第一次加载后将具有
6

会话与cron作业有什么关系?你把牛比作大象。为什么要将
$counter
设置为它以前所在的数组的某个元素?我们需要更多的代码继续
'r'只开放阅读;将文件指针放在文件的开头。
使用
w
进行写入,DB会更好。另外,在哪里增加上一个值?一个可能更简单的选择是
file\u get\u contents
file\u put\u contents
?google analytics或Apache access日志分析器,如awstats@Harshit我想做的是,当我运行php脚本时,我希望$counter递增,然后使用fsomething()保存它,使它继续递增,就像(1,刷新,2,刷新,3)明白了吗?这是特定于查看页面的用户,还是页面通常被查看了多少次?是否必须将文件内容放入变量?函数本身还是写入的值?而不是函数本身。请参阅手册,了解它的详细说明,它在调用时执行$check=file_get_contents('counter.php');if($check<$row['max']){if(isset($check)){$counter2=$check++;}else{$counter2=$check=0;}}文件内容($counter.php',$counter2);我只有2美元;在柜台里。那是你的全部密码吗?如果是这样,您需要关闭
If($check<$row['max']){