Skip to content
Tutorials Center
My learning

Topics

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.

PHP
$handle = fopen("data.txt", "r");
if ($handle === false) {
    // could not open
}

Try it

PHP Write, append and read

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.

PHP Modes and failures

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

PHP
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. Passing false onward gives a TypeError far from the cause.
  • w destroys content immediately, even if you write nothing. Use c or a to keep existing data; use x to avoid overwriting.
  • Close handles with fclose(). PHP closes them at script end, but long-running processes must not leak them. Pair with try/finally (finally).
  • Append is atomic-ish. In a mode small writes go to the end even with several processes; add flock() when you need order and integrity.
  • Reading line by line (fgets()) or in chunks keeps memory low for large files; file() and file_get_contents() load everything.
  • Text and newlines. PHP does not translate line endings on Linux and macOS. On Windows, mode t converts \n to \r\n.
  • Remote URLs work with fopen() only when allow_url_fopen is on; the r mode is the only sensible one. Never pass user-controlled paths: validate and use basename() 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.