Content · Programming · PHP

PHP fopen() Function: Usage and Examples

Opens a file or stream with a chosen mode.

1 views Published Oct 17, 2026 Updated Oct 17, 2026

fopen() Function is one of the PHP operations used in filesystem and stream workflows. Opens a file or stream with a chosen mode. The examples below show what it does and how to handle its result.

What Is fopen() Function?


Opens a file or stream with a chosen mode.

Its syntax is:

fopen(string $filename, string $mode, bool $use_include_path = false, $context = null)

The parameters are:

  • string $filename — The source or destination filesystem path.
  • string $mode — File access mode such as rb for binary reading, wb for writing, or ab for appending.
  • bool $use_include_path = false — When enabled, also searches the configured include_path locations for the requested file.
  • $context = null — Optional PHP stream context controlling relevant stream or wrapper behavior.

Important: Use binary mode when working with binary data, and never allow uncontrolled paths.

Example 1: Basic Usage


The basic example focuses on the behavior of fopen() Function: opens a file or stream with a chosen mode. Inputs are defined locally or shown as application-controlled paths.

<?php

$stream = fopen("php://temp", "w+");
fwrite($stream, "sample");
rewind($stream);
echo stream_get_contents($stream);
fclose($stream);

The displayed value, file content or return status depends on the input and surrounding environment. Results involving local paths, identifiers or generated files are not fixed values.

Example 2: A Different Use Case


This second example illustrates another input or execution condition for fopen() Function. Notice the different checks, arguments, or output handling.

<?php

$stream = fopen(__FILE__, "rb");
if ($stream === false) { throw new RuntimeException("Read failed"); }
echo fread($stream, 12);
fclose($stream);

This variation matters because use binary mode when working with binary data, and never allow uncontrolled paths.

Practical Example: Using the Operation in an Application


The following example demonstrates how fopen() Function fits into a more complete file or archive workflow, including inputs and appropriate checks.

<?php

$filePath = sys_get_temp_dir() . "/example-log.txt";
$stream = fopen($filePath, "ab");
if ($stream === false) { throw new RuntimeException("Cannot open file"); }
try { fwrite($stream, "Event\n"); } finally { fclose($stream); }

The operation shown is an illustrative script. Adapt the filenames, required permissions, and error handling to your own controlled application environment before running it.

Important Behavior and Edge Cases


  • Function behavior: Opens a file or stream with a chosen mode.
  • Limitation: Use binary mode when working with binary data, and never allow uncontrolled paths.

Comparison With Related Functions


Function or MethodDifference
fopen() FunctionOpens a file or stream with a chosen mode.
file_get_contents()Reads data with fewer setup steps; fopen() gives explicit control over stream modes and positioning.

Use fopen() Function for the specific behavior described above. The related operation, file_get_contents(), serves a different purpose; do not substitute it unless that different behavior is intended.

Common Beginner Mistakes


MistakeCorrect Approach
Treating stream or file operations as guaranteed complete on the first call.Use binary mode when working with binary data, and never allow uncontrolled paths. Handle false returns, partial I/O, stream position and closing resources.
Treating file_get_contents() as an interchangeable replacement for fopen() Function.Reads data with fewer setup steps; fopen() gives explicit control over stream modes and positioning.

Key Takeaways


  • Opens a file or stream with a chosen mode.
  • Use binary mode when working with binary data, and never allow uncontrolled paths.
  • Related operation: file_get_contents() — Reads data with fewer setup steps; fopen() gives explicit control over stream modes and positioning.

fopen() Function is useful when its specific behavior matches your application requirement. Keep paths controlled, check failures, and handle environment-dependent operations deliberately.

Share this resourceSend this page to someone who may find it useful.