PHP写入文件两次写入常量

时间:2014-09-05 08:11:43

标签: php google-app-engine

当我尝试执行这个非常简单的代码时

<?php
  $myfile = fopen("db.txt", "at") or die ("Unable to open file!");
  $text = $_GET["name"];
  $text .="\n";
  echo $text;

  fwrite($myfile, $text);
  fwrite($myfile, "abc\n");
  fclose($myfile);
?>

我的档案变成了那样的

coolname
abc
abc

两次“abc \ n”字符串。我答应过,我的代码只有几行。

请求只是http://localhost:8080/helloworld.php?name=coolname

但如果我只是用这个......

  fwrite($myfile, $_GET["name"]);

一切正常,但我想要一个结束线。

有人能解释我为什么会这样吗?

我正在使用PHP AppEngine SDK。 (自我上次PHP体验以来已有9年)

解决方案:

问题在于,当我在浏览器中打开此URL时,它会被处理两次做一些奇怪的事情,我不知道为什么,但我的应用只需要从自定义应用程序调用,所以,从我的应用程序,这段代码只执行一次。不是真正的解决方案,错误仍然存​​在,但它不会影响我的工作流程。

1 个答案:

答案 0 :(得分:0)

你正在使用编写abc的fwrite($myfile, "abc\n");。你是$ _GET还包含abc 否则它写得很好。

<?php 

  $myfile = fopen("YourFile.txt", "at") or die ("Unable to open file!");
  $text = 'hello guys';
  $text .="\n";
  echo $text;

  fwrite($myfile, $text);
  fwrite($myfile, "abc\n");
  fclose($myfile);
?>