<< PHP Read File | PHP Tutorials | PHP Rename a File >>

PHP Write to File

Writing to a file is a very important function in PHP, if you know how to use it properly. Errors in your code could make some of your data disappear due to the fact you weren't appending, you were overwriting. If you want to append to the end of a file I suggest you read the Append to a File at the bottom.

The mode I will be using on this page is the w mode, which writes over the entire file. This basically truncates it and then inserts your data. It will also attempt to create the file if it doesn't exist already.

The fwrite() Function

The fwrite() function is very very similar to the fread() function. Here is an example:

<?php
// Start like any other time
$file_handle = fopen('example.txt', 'w') or die('Error opening file.');

// The data to be inserted
$data = 'This will go into the file.';

// Requires the file handle and the data to be inserted
fwrite($file_handle, $data);
fclose($file_handle);
?>

In the code you start off just like every other time. You have to put the file handle inside the variable so we can deal with it in other functions. This time we used the w mode to write to the file.

Next we put the data we want to put in the file into the $data variable (in case we need it later).

Now it will write the contents of $data into the file contained within $file_handle. It will overwrite all the data that is currently in there. It then closes the handle for the file.

You don't have to be lonely!

As I've said many times the w mode will overwrite the file. But if you use multiple fwrite() functions it won't keep overwriting the data, it moves the pointer to the end of what you've written each time. For example:

<?php
$file_handle = fopen('example.txt', 'w') or die('Error opening file.');

// The data we want in the file, then write it
$data1 = "This is the first line.\n";
fwrite($file_handle, $data1);

$data2 = "This is the second line.\n";
fwrite($file_handle, $data2);

$data3 = "This is the third line.\n";
fwrite($file_handle, $data3);

fclose($file_handle);
echo $data1 . $data2 . $data3;
?>

This will write this to the file (and then output it):

This is the first line.
This is the second line.
This is the third line.

As you can see you can use the \n character to denote a new line. The only thing is, the \n character must be enclosed in double quotes (" ") instead of single quotes (' ') or it won't work properly.

You can use the fwrite() as many times as you want. If you open another handle to the file, all the original data will be lost though!

Append to a File

Appending to a file is essentially the same as writing except the pointer is at the end of the file. It will not overwrite any of the data, instead adding to it. Here is an example:

<?php
$file_handle = fopen('example.txt', 'a') or die('Error opening file.');

// The data to write
$data = "\n\n\nThis is at the end of the file.";

// Write to the file
fwrite($file_handle, $data);

fclose($file_handle);
?>

Then the file should look something like this:

....other data....


This is at the end of the file.

As you can see I used the \n character. Remember that it must be wrapped in double quotes (" ") instead of single quotes (' ').

<< PHP Read File | PHP Tutorials | PHP Rename a File >>