【问题标题】:PHP Add string to text file [duplicate]PHP将字符串添加到文本文件[重复]
【发布时间】:2012-09-09 01:37:17
【问题描述】:

可能重复:
writing to a text file in php

假设我想将字符串 $ip 添加到同一目录中的文本文件 ip.txt 中。我目前正在使用file_get_contents 来读取文本文件。

【问题讨论】:

    标签: php


    【解决方案1】:

    你可以使用file_put_contents

    <?php
      $ip = "foo";
      file_put_contents("ip.txt", $ip, FILE_APPEND);
    ?>
    

    FILE_APPEND追加文本。缺少此标志将导致文件覆盖。

    【讨论】:

    • 文件必须具有什么权限才能从网站写入文件?
    【解决方案2】:

    file_put_contents()FILE_APPEND 标志将文本附加到给定文件:

    file_put_contents('filename.txt', $stringToAppend, FILE_APPEND);
    

    【讨论】:

      【解决方案3】:

      file_put_contents 将内容放入文件中。

      if(!file_put_contents("path/to/file.txt", $ip, FILE_APPEND)){
          // failure
      }
      

      【讨论】:

      • 旧答案,但这需要标记:Warning This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.
      猜你喜欢
      • 1970-01-01
      • 2021-07-25
      • 1970-01-01
      • 1970-01-01
      • 2013-11-25
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 2020-02-16
      相关资源
      最近更新 更多