FOpen PHP Write:FWrite, FClose Example
Date modified - May 16, 2009
This is a short tutorial on how to use the PHP function fopen and write to the file with plain text. So let's dive in. Let's take some simple string and place it in a variable such as:
1 2 3
$string = "Some simple text that will be entered into a file and saved as plain text.";
I'll keep the input parameters in the fopen function to its bare minium (i.e. without the use of options) to keep this tutorial simple. This is the first line:
1 2 3
$handle = fopen("info.php", "w+t");
The first parameter of fopen() is "info.php" which is the filename and the file in which to write the new text and save. The file's location for this example will be in the current directory. If you wish to place it in a different directory then you can use the absolute path such as "/home/username/public_html/backup/" for unix or linux systems or use "c:\\data\\info.php" for windows systems. If you are on a windows system then you would need to escape the backslashes; otherwise, you will receive a PHP error.
The second parameter of fopen() is "w+t". The "w" allows you to write to the file. The PHP describes the "w" option as: "Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it." The "+" sign simply means allowing to read the file contents in addition to writing to the file.
The "t" flag is a text-mode translation flag. There is a "b" flag which is a binary-mode translation flag. Unless you use images and other binary-type files then indicate the "t" flag. The PHP site describes this in more detail as: "Windows offers a text-mode translation flag ('t') which will transparently translate \n to \r\n when working with the file. In contrast, you can also use 'b' to force binary mode, which will not translate your data. To use these flags, specify either 'b' or 't' as the last character of the mode parameter."
"The default translation mode depends on the SAPI and version of PHP that you are using, so you are encouraged to always specify the appropriate flag for portability reasons. You should use the 't' mode if you are working with plain-text files and you use \n to delimit your line endings in your script, but expect your files to be readable with applications such as notepad. You should use the 'b' in all other cases."
1 2 3
The fwrite function's parameter is pretty straight forward. The first parameter, "$handle" , is the "system file pointer resource". In other words, just think of "$handle" as a reference variable to the text file (with the option "w+" and flag "t") created by the fopen().
Obviously, the second paramter, "$string" is the text (i.e. content) to write to the file. In this case, the "$string" variable is "Some simple text that will be entered into a file and saved as plain text." Nothing too complicated.
1 2 3
You should always close your reference or as the PHP site describes the "$handle" variable the "system file pointer resource". You can run into problems if you do not. For instance, if you do not close the reference then it could lock the file and disable other users who are trying to read or write to the file.
If you would like to try the try this out code out then copy and paste this in a php file and call it something like "fwriteTest.php" and then execute it via the web browser. You should see a new file called "info.php" in the same directory where you created the "fwriteTest.php" file. Here's the entire code:
1 2 3 4 5
$handle = fopen("info.php", "w+t"); fwrite($handle, $secondStr); fclose($handle);