0
votes

I use file_put_content to write data in file every time there is an entry in database. Instead of appending new entry it overwrites old data with new.Here is part of code which I am using.I tried with APPEND In flag but doesn't work. `

  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);




  }

  }
  ?>      `
2
Not clear your question, explain in detail.... - Kausha Mehta
@KaushaMehta rather than appending new entry in trunk_test.conf file.its overwrites previous entry and writes new.so data only for one entry.I want for all entry in db - soni8010
ok, i'm going to add answer..... - Kausha Mehta
you want to append new data into the file ?? - Andrew

2 Answers

5
votes

If you want to append new data into the file

you should use this flag

// 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); 
0
votes

Put below code after foreach(){ //your code for getting data and put into single variable }. Remove the same from inside foreach and remove $content2 = ""; too.

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