Write a string to a file

Write a string to a file
This example shows you how to write a string to a file using PHP, and also do some error handling to ensure that your file gets written correctly.
1. // Open the file as writeable (create it if it doesn't exist)
2. if (!$FileHandle = fopen($FilePath,"w"))
3. {

4.        die("Could not create/open the file.");
5. }
6. // Write a string to the file
7. if (!fwrite($FileHandle, "This will be written to the file"))
8. {

9.        die("Could not write to the file.");
10. }
11. // Close the file
12. fclose($FileHandle);

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top