Opening a stream

[fopen](<http://php.net/fopen>) opens a file stream handle, which can be used with various functions for reading, writing, seeking and other functions on top of it. This value is of resource type, and cannot be passed to other threads persisting its functionality.

$f = fopen("errors.log", "a"); // Will try to open errors.log for writing

The second parameter is the mode of the file stream:

Mode | Description | —: | :––––– |r | Open in read only mode, starting at the beginning of the file |r+ | Open for reading and writing, starting at the beginning of the file |w | open for writing only, starting at the beginning of the file. If the file exists it will empty the file. If it doesn’t exist it will attempt to create it. |w+ | open for reading and writing, starting at the beginning of the file. If the file exists it will empty the file. If it doesn’t exist it will attempt to create it. |a | open a file for writing only, starting at the end of the file. If the file does not exist, it will try to create it |a+ | open a file for reading and writing, starting at the end of the file. If the file does not exist, it will try to create it |x | create and open a file for writing only. If the file exists the fopen call will fail |x+ | create and open a file for reading and writing. If the file exists the fopen call will fail |c | open the file for writing only. If the file does not exist it will try to create it. It will start writing at the beginning of the file, but will not empty the file ahead of writing |c+ | open the file for reading and writing. If the file does not exist it will try to create it. It will start writing at the beginning of the file, but will not empty the file ahead of writing |

Adding a t behind the mode (e.g. a+b, wt, etc.) in Windows will translate "\\n" line endings to "\\r\\n" when working with the file. Add b behind the mode if this is not intended, especially if it is a binary file.

The PHP application should close streams using [fclose](<http://php.net/fclose>) when they are no longer used to prevent the Too many open files error. This is particularly important in CLI programs, since the streams are only closed when the runtime shuts down – this means that in web servers, it may not be necessary** (but still should, as a practice to prevent resource leak) to close the streams if you do not expect the process to run for a long time, and will not open many streams.

Reading

Using [fread](<http://php.net/fread>) will read the given number of bytes from the file pointer, or until an EOF is met.

Reading lines

Using [fgets](<http://php.net/fgets>) will read the file until an EOL is reached, or the given length is read.

Both [fread](<http://php.net/fread>) and [fgets](<http://php.net/fgets>) will move the file pointer while reading.

Reading everything remaining

Using [stream_get_contents](<http://php.net/stream-get-contents>) will all remaining bytes in the stream into a string and return it.

Adjusting file pointer position

Initially after opening the stream, the file pointer is at the beginning of the file (or the end, if the mode a is used). Using the [fseek](<http://php.net/fseek>) function will move the file pointer to a new position, relative to one of three values:

[rewind](<http://php.net/rewind>) is a convenience shortcut of fseek($fh, 0, SEEK_SET).