PHP fopen( ) Function - GeeksforGeeks

PHP fopen( ) Function

Last Updated : 29 Oct, 2026

The fopen() function in PHP opens a file or URL and returns a file handle that can be used with functions such as fread(), fwrite(), and fclose(). The specified mode determines whether the file can be read, written, or both.

Syntax:

fopen(
string $filename,
string $mode,
bool $use_include_path = false,
?resource $context = null
): resource | false

Parameters:

  • $filename: Specifies the file path or URL to open.
  • $mode: Specifies the access mode, such as reading, writing, or appending.
  • $use_include_path: Optional Boolean parameter. Set it to true to search for the file in PHP's include_path.
  • $context: Optional stream context that specifies additional options for accessing the file or URL.

Return Value:

  • Returns a file handle (resource) when the file opens successfully.
  • Returns false if the file cannot be opened.

Different File Modes

The $mode parameter determines how PHP opens a file.

ModeDescription
rOpens an existing file for reading. The pointer starts at the beginning.
r+Opens an existing file for reading and writing. The pointer starts at the beginning without truncating the file.
wOpens a file for writing, truncates existing content, or creates the file if it does not exist.
w+Opens a file for reading and writing, truncates existing content, or creates the file if it does not exist.
aOpens a file for writing at the end, preserving existing content. Creates the file if it does not exist.
a+Opens a file for reading and writing, with writes directed to the end. Creates the file if it does not exist.
xCreates a new file for writing. Fails if the file already exists.
x+Creates a new file for reading and writing. Fails if the file already exists.

Note: The w and w+ modes erase existing file content when the file is opened.

Opens a File for Reading

The following example opens an existing file in read-only mode.

example.txt:

File opened successfully.

read.php:

php
<?php
$file = fopen("example.txt", "r");

if ($file !== false) {
    echo "File opened successfully.";
    fclose($file);
} else {
    echo "Failed to open file.";
}
?>
  • fopen() opens example.txt in read mode.
  • The condition checks whether the function returned a valid file handle.
  • fclose() closes the file after use.

Note: The file example.txt must exist in the specified location for this example to succeed.

Output:

Screenshot-2026-10-29-111725

Writes Data to a File Using fopen()

The w mode opens a file for writing and removes its existing content. If the file does not exist, PHP attempts to create it.

php
<?php
$file = fopen("sample.txt", "w");
if ($file) {
    fwrite($file, "This is a sample text.");
    fclose($file);
    echo "Data written to file successfully.";
} else {
    echo "Failed to open file for writing.";
}
?>
  • fopen() opens example.txt in write mode.
  • fwrite() writes the specified text to the file.
  • The return value of fwrite() is checked to determine whether the write operation succeeded.
  • fclose() closes the file.

Output:

Screenshot-2026-10-29-112532
Terminal Output


Screenshot-2026-10-29-112356
sample.txt

Appends Data to a File Using fopen()

The a mode adds new content at the end of a file without removing its existing content.

php
<?php
$file = fopen("sample.txt", "a");

if ($file !== false) {
    $result = fwrite($file, "\nAppended text.");
    fclose($file);

    if ($result !== false) {
        echo "Data appended to file successfully.";
    } else {
        echo "Failed to append data.";
    }
} else {
    echo "Failed to open file for appending.";
}
?>
  • fopen() opens example.txt in append mode.
  • fwrite() adds the new text at the end of the file.
  • The existing content remains unchanged.
  • fclose() closes the file after the operation.

Output:

Screenshot-2026-10-29-112924
Terminal Output


Screenshot-2026-10-29-113005
sample.txt

Common Issues with PHP fopen()

  • File not found: Opening a nonexistent file in r mode fails because the file must already exist.
  • Permission denied: PHP may fail to open a file if the process lacks the required permissions.
  • Incorrect mode: Using w instead of a, for example, truncates existing content instead of preserving it.
  • Invalid path: An incorrect or inaccessible file path can cause the operation to fail.
  • URL access restrictions: Opening URLs depends on PHP configuration, stream-wrapper support, and the remote resource's availability.
Comment