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';
?>