PHP file write

Use the following code to write contents to a file.

<?php
    $file_path = "";
    $contents = "";
    
    file_put_contents($file_path, $contents);​
?>

If you want to divide the operation into several, smaller rewrites, then you can use the following code:

<?php
    $file_path = "";
    
    $file_header = fopen($file_path, 'w') or die("Cannot access $file_path");
    fwrite($file_header, "This will result");
    fwrite($file_header, " ");
    fwrite($file_header, "in a single");
    fwrite($file_header, " ");
    fwrite($file_header, "lined file.");
    fclose($file_header);​
?>

Thanks to Jaisen Mathai for his suggestion on using the “file_put_contents” function.