Pampour

Inserting lines to a file

Mar 29th, 2014
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.25 KB | None | 0 0
  1. <?php
  2. $f = "test.txt";
  3.  
  4. // read into array
  5. $arr = file($f);
  6.  
  7. // add second line
  8. array_splice($arr,1,0, array("test line 2\r\n"));
  9.  
  10. // reindex array
  11. $arr = array_values($arr);
  12.  
  13. // write back to file
  14. file_put_contents($f,implode($arr));
  15. ?>
Advertisement
Add Comment
Please, Sign In to add comment