duanmei1894
2017-04-06 13:22
浏览 74
已采纳

在多个文本文件中递增计数器

I'm trying to increment a counter in a multiple text file when a user visit my page, but the code I'm working is not working below is the code

$files = glob("counters/visit/*.txt");


foreach($files as $file) {
    $content = file_get_contents($file);


    if(!isset($_SESSION['hasVisited'])){
      $_SESSION['hasVisited']="yes";
      $content++;
      $f = fopen($files, "w");
      fwrite($f, $content);
      fclose($f);
    }
}

图片转代码服务由CSDN问答提供 功能建议

我正在尝试在用户访问我的页面时增加多文本文件中的计数器,但代码为I 工作不起作用是代码

  $ files = glob(“counter / visit / * .txt”); 
 
 
foreach($ files as  $ file){
 $ content = file_get_contents($ file); 
 
 
 if(!isset($ _ SESSION ['hasVisited'])){
 $ _SESSION ['hasVisited'] =“yes”;  
 $ content ++; 
 $ f = fopen($ files,“w”); 
 fwrite($ f,$ content); 
 fclose($ f); 
} 
} 
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongwubao4785 2017-04-06 13:28
    已采纳

    First make sure that $contentis an integer by doing the following :

    $content = intval(file_get_contents($file));
    

    Then you're using :

    $f = fopen($files, "w");

    Instead of :

    $f = fopen($file, "w");

    fopen can't accept an array as parameter

    Also as mentioned by @alanlittle, if you want all your files to be incremented, you should think about the moment where you set $_SESSION['hasVisited']="yes"; and put it at the end of the loop.

    打赏 评论

相关推荐 更多相似问题