duangou2028 2018-01-15 08:59
浏览 82
已采纳

如何将新行中的内容添加到txt文件中

I would like to create a < input> where if someone enters text, a text file will add the content entered as a new row. I have tried highly modified the feature in this link: here

  • 写回答

1条回答 默认 最新

  • dowy77780 2018-01-15 09:02
    关注

    Just use the PHP_EOL (PHP end of line) constant that will create a new line.
    This must be appended at the end of each line.

    $file = fopen("myfile.txt", "a+");
    fwrite($file, "hello".PHP_EOL);
    // or...
    fwrite($file, $myvar.PHP_EOL);
    

    Alternatively, you could create your own, new, function:

    function fwrite2($handle, string $string, $length = null, $newline = true) {
        $string = $newline ? $string.PHP_EOL : $string;
    
        if (isset($length)) {
            fwrite($handle, $string, $length);
        } else {
            fwrite($handle, $string);
        }
    }
    

    Call the above in the same manner, except the third argument will now create a new line automatically.


    Edit following the comments:

    The a+ means that the file is open and stored in $file and is available for reading and writing. The a stands for append; meaning the fwrite will append the file.
    See more on the PHP documentation.

    $file = fopen("myfile.txt", "a+");
    fwrite2($file, "{$_GET['message']} | from {$_GET['sender']}");
    

    Since you are using the URL to send data (ill-advised, but that is another point completely), you can access its contents through the superglobal variable - $_GET.
    Notice that I have wrapped the values in curly braces. This is because $_GET is an array and if you want to interpolate arrays they must be wrapped, the same goes for class properties.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器