PHP Reference ยท Files and I/O
PHP fopen() function
Reference for PHP fopen(): open a file or stream, every mode (r, w, a, x, c, +), return value and failure, php://memory and php://stdout, and closing handles.
fopen() opens a file or another stream and returns a handle, a resource-like object you pass to fread(), fwrite(), fgets(), fclose() and friends. The mode string says whether you want to read, write or append, and what to do if the file does or does not exist.
Use fopen() when you need control: reading a large file line by line, appending to a log, or writing in pieces. For small files, file_get_contents() and file_put_contents() are shorter.
$handle = fopen("data.txt", "r"); if ($handle === false) { // could not open }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
fopen( string $filename, string $mode, bool $use_include_path = false, ?resource $context = null ): resource|false
| Name | Description |
|---|---|
$filename |
A path or a URL-style stream wrapper such as php://memory, php://stdin or php://output. |
$mode |
How to open it; see the table below. |
$use_include_path |
If true, also search the include_path. |
$context |
An optional stream context from stream_context_create(). |
| Mode | Meaning |
|---|---|
r / r+ |
Read / read and write. The file must exist. Position at the start. |
w / w+ |
Write / read and write. Creates the file or truncates it to zero length. |
a / a+ |
Append / read and append. Creates the file; writes always go to the end. |
x / x+ |
Create and write; fails if the file already exists. |
c / c+ |
Open for writing, creating it if needed, without truncating; position at the start. |
Add b (binary) or t to control text translation; b is recommended on Windows and harmless elsewhere. e sets close-on-exec.
Return value: the stream resource, or false with a warning if the file can't be opened. Check with === false.
Usage notes
- Always test for
false. Passingfalseonward gives aTypeErrorfar from the cause. wdestroys content immediately, even if you write nothing. Usecorato keep existing data; usexto avoid overwriting.- Close handles with
fclose(). PHP closes them at script end, but long-running processes must not leak them. Pair withtry/finally(finally). - Append is atomic-ish. In
amode small writes go to the end even with several processes; addflock()when you need order and integrity. - Reading line by line (
fgets()) or in chunks keeps memory low for large files;file()andfile_get_contents()load everything. - Text and newlines. PHP does not translate line endings on Linux and macOS. On Windows, mode
tconverts\nto\r\n. - Remote URLs work with
fopen()only whenallow_url_fopenis on; thermode is the only sensible one. Never pass user-controlled paths: validate and usebasename()or an allow list. - Useful wrappers:
php://stdin,php://stdout,php://stderr,php://memory,php://temp(spills to disk),php://output. - Permissions and locks: failures are usually a missing directory, permissions or an open lock;
error_get_last()tells which.