https://dev.webpages.dk/  
 
PHP How to Write to a File
How to Create a File:

Here we create a file, and write to it in the same go.
To the right you see the options you have for use witht the 'fopen'.
This can be Read, Write, and Append.
And at the beginning or the end of the file.

Notice that using 'W' or 'W+' will let you create the file, when writing to it, if it doesn not already exist.

 Write to a File

How do you write to a file using PHP?

.. this could be any filetypes that are flatfiles. Like PHP, CSS, HTML, XML, TXT, Javascripts, and many many more.

fopen have following options;

'r' (read only, file pointer at the beginning)
'r+' (read and write, file pointer at the beginning)
'w' (write only, file pointer at the beginning, zero length file, create it if it does not exist)
'w+' (read and write, file pointer at the beginning, zero length file,
create it if it does not exist)
'a' (write only, file pointer at the end, zero length file)
'a+' (read and write, file pointer at the end, zero length file)
'x' (write only, file pointer at the beginning, if exists, return FALSE)
'x+' (read and write, file pointer at the beginning, if exists, return FALSE)

 PHP

<?php
    $String = "Hello World!\n\nThis is a test in creating a textfile, and writing to it."; 

    $mytextfile = fopen("file.txt", "a") or die("An error occurred. Send webmaster a message.."); 

    fwrite($mytextfile, $String); 
    fclose($mytextfile); 

    echo 'Super, you have created a file'; 
?>
Icons made by Freepik from www.flaticon.com This example shows you a way to write to a file using PHP. See the options of what will suit your use.
13:20:04