Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Add string to text file [duplicate]

Tags:

php

Possible Duplicate:
writing to a text file in php

Say I wanted to add the string $ip to the text file ip.txt in the same directory. I am currently using file_get_contents to read from the text file.

like image 554
CJ Sculti Avatar asked Sep 09 '12 01:09

CJ Sculti


2 Answers

You could use file_put_contents

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

FILE_APPEND will append the text. Absence of this flag will cause file-overwriting.

like image 80
DavChana Avatar answered Sep 21 '22 15:09

DavChana


file_put_contents() with FILE_APPEND flag appends text to given file:

file_put_contents('filename.txt', $stringToAppend, FILE_APPEND); 
like image 43
moonwave99 Avatar answered Sep 23 '22 15:09

moonwave99