PHP: Write to a file

The following code writes to a file, it will overwrite the current contents of the file:


$string = "file contents goes here";
$myFile = "file.php";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $string);
fclose($fh);

Make sure you give file.php write access on the server (Apache chmod 775 normally works).

Leave a Reply