Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Add contents of 1 file to the top of another file without overwrite in PHP?

i stuck on here . i have function which rename the my text files when first one is amended.

function copyFiles(string $newLine) : void
{

    rename( "file4.txt", "file5.txt");    
    rename( "file3.txt", "file4.txt");    
    rename("file2.txt", "file3.txt");
    rename("file1.txt", "file2.txt");

file_put_contents("file1.txt",$newLine."\n", FILE_APPEND|LOCK_EX);
}

and i need to take content from the last file5.txt
and put it in another text file named fullnews.txt

if the content in fullnews.txt is Hello World and in file5.txt is Good afternoon World

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

after when i amend the first file i need fullnews.txt to look like

Good afternoon World
Hello World

>Solution :

I think the best way to go about this for such small strings is to read and store the contents of the file5.txt, and then append the read content to fullnews.txt.

$content = file_get_contents("file5.txt");
file_put_contents("fullnews.txt", $content, FILE_APPEND);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading