PHP fopen function tutorial shows how to open files in PHP. Learn fopen with practical examples including strict typing.
last modified April 3, 2025
The PHP fopen function opens a file or URL for reading or writing. It’s fundamental for file handling operations in PHP applications.
The fopen function creates a file pointer resource connected to the specified file or URL. It requires a filename and mode parameter.
Syntax: fopen(string $filename, string $mode, bool $use_include_path = false, ?resource $context = null): resource|false.
The function returns a file pointer resource on success, or false on failure. Always check the return value before proceeding with file operations.
This example demonstrates opening a file in read-only mode.
read_file.php
<?php
declare(strict_types=1);
$filename = “example.txt”; $handle = fopen($filename, “r”);
if ($handle === false) { throw new RuntimeException(“Failed to open file: {$filename}”); }
// Read and process the file here fclose($handle);
The “r” mode opens the file for reading only. We check if the operation succeeded before proceeding. Always close files with fclose when done.
This example shows how to create a new file and write content to it.
write_file.php
<?php
declare(strict_types=1);
$filename = “output.txt”; $handle = fopen($filename, “w”);
if ($handle === false) { throw new RuntimeException(“Failed to create file: {$filename}”); }
$bytes = fwrite($handle, “Hello, World!\n”); if ($bytes === false) { throw new RuntimeException(“Failed to write to file”); }
fclose($handle);
The “w” mode creates the file if it doesn’t exist or truncates it to zero length if it does. We write a string and check the return value.
This example demonstrates how to append content to an existing file.
append_file.php
<?php
declare(strict_types=1);
$filename = “log.txt”; $handle = fopen($filename, “a”);
if ($handle === false) { throw new RuntimeException(“Failed to open file for appending: {$filename}”); }
$logEntry = date(‘Y-m-d H:i:s’) . " - User logged in\n"; $bytes = fwrite($handle, $logEntry); if ($bytes === false) { throw new RuntimeException(“Failed to append to file”); }
fclose($handle);
The “a” mode opens the file for writing only, placing the pointer at the end of the file. This is ideal for log files and similar use cases.
This example shows how to open a file for both reading and writing.
read_write_file.php
<?php
declare(strict_types=1);
$filename = “data.txt”; $handle = fopen($filename, “r+”);
if ($handle === false) { throw new RuntimeException(“Failed to open file for read/write: {$filename}”); }
$content = fread($handle, filesize($filename)); if ($content === false) { throw new RuntimeException(“Failed to read file”); }
// Modify content $newContent = strtoupper($content);
rewind($handle); $bytes = fwrite($handle, $newContent); if ($bytes === false) { throw new RuntimeException(“Failed to write to file”); }
fclose($handle);
The “r+” mode opens the file for both reading and writing. We read the content, modify it, then write it back after rewinding the pointer.
This example demonstrates opening a remote file using HTTP.
remote_file.php
<?php
declare(strict_types=1);
$url = “https://example.com/data.json"; $handle = fopen($url, “r”);
if ($handle === false) { throw new RuntimeException(“Failed to open remote file: {$url}”); }
$content = stream_get_contents($handle); if ($content === false) { throw new RuntimeException(“Failed to read remote file”); }
// Process the remote content $data = json_decode($content, true); if (json_last_error() !== JSON_ERROR_NONE) { throw new RuntimeException(“JSON decode error: " . json_last_error_msg()); }
fclose($handle);
fopen can open URLs when allow_url_fopen is enabled in php.ini. We read the remote content and process it as JSON in this example.
Error Handling: Always check fopen’s return value.
Resource Cleanup: Use fclose or try-finally blocks.
File Permissions: Ensure proper permissions for operations.
Security: Validate filenames, especially user-provided ones.
Memory: For large files, read in chunks rather than all at once.
This tutorial covered the PHP fopen function with practical examples showing various file opening modes and use cases.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.