Home PHP Filesystem fopen() PHP fopen() Function Definition In PHP, fopen() opens a file or a URL and gives you a handle that other functions, such as fgets(), fwrite() and fclose(), can use.
The mode you pass decides what you can do with the file, such as read only, write, or append. After you are done, close the handle with fclose().
Syntax Call fopen() with the values you need. Start with this line.
$handle = fopen('notes.txt', 'r'); Call What it does fopen('notes.txt', 'r')Opens an existing file for reading only, starting at the beginning. fopen('notes.txt', 'w')Opens the file for writing and clears its content. The file is created if needed. fopen('log.txt', 'a')Opens the file for writing and adds new text to the end.
Common modes are r (read), w (write and clear), a (append), x (create, fail if it exists) and c (write without clearing). Add a plus sign, as in r+, to allow both reading and writing. On Windows, add b, as in rb, for binary files such as images.
Library declarations fopen(string $filename, string $mode, bool $use_include_path = false, ?resource $context = null): resource|false Parameters Parameter Description filename Required. The path of the file or a URL such as php://memory. mode Required. A short text that sets how the file is opened, for example r, w, a, x or c. use_include_path Optional. Set to true to search for the file in the include_path as well. The default is false. context Optional. A stream context created with stream_context_create(). The default is null.
Return Value Return Value Description resource Returns a file handle on success. In PHP 8 this is a resource that you pass to other file functions. false Returns false on failure, for example when the file is missing in r mode or the folder is not writable.
Important
Always check that fopen() did not return false before you use the handle, and always call fclose() when you finish. The w mode erases the file content straight away, so be careful not to use it by mistake.
PHP Examples 1. Write and Read a File 2. Open a File That Is Missing 3. Append Lines to a Log Example 1 — Write and Read a File Open a file to write a line, close it, and open it again to read the line back.
<?php
$file = __DIR__ . '/fopen_demo.txt';
$fp = fopen($file, 'w');
fwrite($fp, 'Hello from fopen!');
fclose($fp);
$fp = fopen($file, 'r');
echo fgets($fp), PHP_EOL;
fclose($fp);
unlink($file); How It Works fopen() in w mode creates the file and fwrite() puts the text inside it. fclose() closes the handle so the data is saved. fopen() in r mode opens the same file for reading. fgets() reads the first line, which is printed, and the handle is closed again. Example 2 — Open a File That Is Missing In r mode, fopen() returns false when the file does not exist. Always test the handle.
<?php
$fp = @fopen(__DIR__ . '/missing.txt', 'r');
if ($fp === false) {
echo "Could not open the file", PHP_EOL;
} else {
echo fgets($fp);
fclose($fp);
} How It Works The r mode never creates a file, so a missing file cannot be opened. fopen() returns false and the @ sign hides the PHP warning. The if check stops the script before fgets() receives an invalid handle. A friendly message is shown, and no fclose() is needed because nothing was opened. Frequently Asked Questions What does fopen() do in PHP? It opens a file or URL and returns a handle. You use the handle with functions like fgets(), fwrite() and fclose().
What is the difference between the r, w and a modes? r reads an existing file, w writes and erases the old content first, and a writes at the end and keeps the old content.
Why does fopen() return false? Usually the file does not exist in r mode, the folder is not writable, or the path is wrong. Check the path and the folder permissions.
Do I need to call fclose() after fopen()? Yes, it is a good habit. PHP closes open files at the end of the script, but closing early frees resources and makes sure the data is saved.
When should I use file_get_contents() instead of fopen()? Use file_get_contents() when you only need to read the whole file at once. Use fopen() when you want to read or write step by step, or lock the file.
Next: fpassthru() fpassthru() reads everything left in an open file and sends it straight to the output.
fpassthru() tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion