php - file_put_contents 覆盖数据

标签 php file-put-contents

每当数据库中有一个条目时,我都会使用 file_put_content 将数据写入文件中。它不是附加新条目,而是用新数据覆盖旧数据。这是我正在使用的代码的一部分。我尝试使用 APPEND In 标志,但不起作用。 `

  include_once(CLASS_PATH."fetch_service.php"); 
  $objfetch = new fetchService();


  $res = $objfetch->fetch_proxy("*","where account_id='".$_SESSION['account_id']."'");
  foreach($res as $result)
  {
  $proxyname=$result['proxyname'];
  $ip=$result['proxyip'];
  $proxyport=$result['port'];
  $proxyuser=$result['username'];
  $proxypwd=$result['password'];
  $proxydomain=$result['domain'];
  $account_id=$result['account_id'];


  $content2 = "";
  $content2 .= ";/*----*-------------------*----------"."\n";
  $content2 .= "; * Created On ".date("Y-m-d")."\n";
  $content2 .= "; * Adding Proxy"."\n";
  //                $content2 .= "; * By ".$_SESSION['username']."\n";
  $content2 .= "; *----*-------------------*----------"."\n\n";



  $content2 .="[$proxyname]"."\n"; //context Name//
  $content2 .="username=$proxyuser"."\n";
  $content2 .="secret=$proxypwd"."\n";
  $content2 .="fromdomain=$proxydomain"."\n";
  $content2 .="host=$ip"."\n";
  $content2 .="port='$proxyport'"."\n";


  $content2 .="canreinvite=yes"."\n";
  $content2 .="nat=force_rport,comedia"."\n";
  $content2 .="type=peer"."\n";
  $content2 .="disallow=all"."\n";
  $content2 .="allow=g729"."\n";
  $content2 .="allow=ulaw"."\n";
  $content2 .="allow=alaw"."\n";


  $file2 = 'trunk_test.conf';
  error_log("======FILE path ====$file2======");
  file_put_contents($file2, $content2, LOCK_EX);




  }

  }
  ?>      `

最佳答案

如果您想将新数据追加到文件中

你应该使用这个标志

// this will append the content to the end of the file, 
// and prevent anyone else writing to the file at the same time
file_put_contents($file2, $content2, FILE_APPEND | LOCK_EX); 

关于php - file_put_contents 覆盖数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56406080/

相关文章:

php - 根据上一行的日期获取下一行的内容

php - Foreach 循环仅返回多维数组 PHP 上的第一个索引

php - 如何使用 file_get_contents() 加载远程文件?

php - file_put_contents 不适用于 json laravel 和 php

php - 如何使用 mysql 获得表中的最大总和

php - 帮助倒计时 jquery 脚本接受 unix 时间戳作为输入

php - 数据库异常应该致命吗?

php - 如何在 php 中安装 ffmpeg

php - file_put_contents() : connect() failed: Permission denied in AWS server

php - 如何使用具有特定结构的php从文本文件中删除一行