Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,907 questions

51,839 answers

573 users

How to append text to the end of a text file in PHP

3 Answers

0 votes
/*
resource fopen ( string $filename , string $mode [, bool $use_include_path = false 
                                                 [, resource $context ]] )
*/

// mode: 'a' - Open for writing only. Place the file pointer at the end of the file. 

$f = fopen("e:/datefile.txt", "a") or die("Error open file!");
$txt = "\r\nIs Popular";
fwrite($f, $txt);
fclose($f);

/*
run:

PHP
Programming
For Web Applications
Is Popular

*/

 



answered Dec 3, 2015 by avibootz
edited Dec 14, 2015 by avibootz
0 votes
/*
int file_put_contents ( string $filename , mixed $data [, int $flags = 0 
                                                       [, resource $context ]] )
*/

// FILE_APPEND - append the content to the end of the file
// LOCK_EX - prevent anyone else writing to the file at the same time
// The file_put_contents() function is the same as calling fopen(), fwrite() and fclose() 

file_put_contents("e:/test.txt", " New Text", FILE_APPEND | LOCK_EX);

    
/*
run:


*/

 



answered Dec 12, 2015 by avibootz
edited Nov 29, 2017 by avibootz
0 votes
$file = "d:\data.txt";
$content = file_get_contents($file);

$content .= "\r\n<?php\r\n";
$content .= "function f(){}\r\n";
$content .= "?>\r\n";

file_put_contents($file, $content);

/*
run: 

data.txt:
---------
<!doctype html>
<html>
<head>

<?php
function f(){}
?>


*/

 



answered Jun 17, 2016 by avibootz

Related questions

1 answer 172 views
1 answer 169 views
1 answer 141 views
1 answer 222 views
222 views asked Aug 15, 2014 by avibootz
1 answer 142 views
142 views asked Jul 7, 2020 by avibootz
2 answers 191 views
1 answer 211 views
...