1. Create a text file (in this case we’ll name it test.txt)
  2. Make it writable (permissions: 0777)
  3. PHP Code:
  4. <?php
    
    function writeToFile(){
    
      // 'w' overwrites entire file whereas 'a' appends text to it
    
      $filename = 'test.txt';
      if (is_writable($filename)) {                                                            
        $Handle = fopen($filename, 'a');
        $Data = "line 1\n".
        "line 2"; 
        fwrite($Handle, $Data); 
        print "Data Written"; 
        fclose($Handle);
      }
      else {
        echo "It's not writable!";
      }
    
    }
    
    ?>
    
  5. Now call your function wherever you want:
    <?php writeToFile(); ?>