A list of possible modes for fopen() using mode

$file_handle = fopen(“test.txt”, “r“);

Here’s a fuller list of things you can replace “r” with.

Mode Meaning
r Use this to read a file only. The pointer is set to the
start of the file.
r+ Use this to read and write to a
file. The pointer is set to the start of the file.
w Use this to write to a file only. It will erase the entire
contents of the file you have open. If no file exists with your chosen name,
then it will create one for you
w+ Same as “w”, but used
to read and write.
a Use this to write to a file only, and append data to the
end of the file. Doesn’t erase contents, in other words.
a+ Same as “a”, but with
read access as well.
x Create a file to write only. But gives you a special warning
called E_WARNING.
x+ Same as x but with read access as
well.
t In Windows, a line break is \r\n. The t converts \n line
breaks created on other Operating Systems so that they are readable with
Windows
b Force PHP to open the file in binary
mode.

So if you wanted to read and write to the file, you’d use this:

$file_handle = fopen(“test.txt”, “r+”);

Or this, if you want to append data to the end of file when you’re writing it back:

$file_handle = fopen(“test.txt”, “a+”);

If you need to work with binary files (like images), then you can add the “b”:

$file_handle = fopen(“test.txt”, “rb”);