PHP fopen() Function
The PHP fopen() function opens a file or an URL. The function binds a named resource, specified by filename, to a stream. The mode parameter is used to specify the type of access required with the stream. The function returns a file pointer resource on success, or false on failure. The error output can be hidden by adding an '@' in front of the function name.
Syntax
fopen(filename, mode, use_include_path, context)
Parameters
filename |
Required. Specify the file or URL to open. |
mode |
Required. This parameter specifies the type of access required to the stream. The possible values of modes are described in the table below: |
use_include_path |
Optional. Set to '1' or true if you want to search for the file in the include_path, too. include_path can be set in php.ini. |
context |
Optional. Specify the context of the file handle. Context is a set of options that can modify the behavior of a stream. |
list of modes
Modes | Description |
---|---|
"r" | Opens a file for reading only. Places the file pointer at the beginning of the file. |
"w" | Opens a file for writing only. Places the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, it attempts to create it. |
"a" | Opens a file for writing only. Places the file pointer at the end of the file. If the file does not exist, it attempts to create it. In this mode, fseek() has no effect, writes are always appended. |
"x" | Creates and opens a file for writing only. Places the file pointer at the beginning of the file. If the file already exists, it returns false and generates an error of level E_WARNING. |
"c" | Creates and opens a file for writing only. Places the file pointer at the beginning of the file. If the file does not exist, it attempts to create it. If the file exists, it is neither truncated (as opposed to 'w'), nor generates an error (as is the case with 'x'). This may be useful if it is desired to get an advisory lock (see flock() function) before attempting to modify the file. |
"r+" | Opens a file for reading and writing. Otherwise it has the same behavior as 'r'. |
"w+" | Opens a file for reading and writing. Otherwise it has the same behavior as 'w'. |
"a+" | Open a file for reading and writing. Places the file pointer at the end of the file. If the file does not exist, it attempts to create it. In this mode, fseek() only affects the reading position, writes are always appended. |
"x+" | Creates and opens a file for reading and writing. Otherwise it has the same behavior as 'x'. |
"c+" | Creates and opens a file for reading and writing. Otherwise it has the same behavior as 'c'. |
"e" | Set close-on-exec flag on the opened file descriptor. Only available in PHP compiled on POSIX.1-2008 conform systems. |
Return Value
Returns a file pointer resource on success, or false on failure.
Exceptions
Upon failure, an E_WARNING is thrown.
Example: Open a file using read mode
Lets assume that we have a file called test.txt. This file contains following content:
This is a test file. It contains dummy content.
In the example below, the file is opened using fopen() function with 'r' mode. This places the file pointer at the beginning of the file. After performing the reading operation, it is closed using fclose() function.
<?php //open the file in read mode $fp = fopen("test.txt", "r") or die("Unable to open file!"); //reading the file line by line //and displaying the read content while(!feof($fp)) { echo fgets($fp); } //close the file fclose($fp); ?>
The output of the above code will be:
This is a test file. It contains dummy content.
Example: Open a file using write mode
When the file is opened using 'w' mode, the function places the file pointer at the beginning of the file and deletes its previous content. Consider the example below:
<?php //open the file in write mode $fp = fopen("test.txt", "w"); //write some content to it fwrite($fp, 'A fresh content is added.'); //close the file fclose($fp); //open the file in read mode $fp = fopen("test.txt", "r") or die("Unable to open file!"); //reading the file line by line //and displaying the read content while(!feof($fp)) { echo fgets($fp); } //close the file fclose($fp); ?>
The output of the above code will be:
A fresh content is added.
Example: Open a file using append mode
When the file is opened using 'a' mode, the file pointer is placed at the end of the file. Hence, fwrite() writes to the end of the file.
<?php //open the file in append mode $fp = fopen("test.txt", "a") or die("Unable to open file!"); //append third line to the file fwrite($fp, PHP_EOL.'Third line is added.'); //close the file fclose($fp); //open the file in read mode $fp = fopen("test.txt", "r") or die("Unable to open file!"); //reading the file line by line //and displaying the read content while(!feof($fp)) { echo fgets($fp); } //close the file fclose($fp); ?>
The output of the above code will be:
This is a test file. It contains dummy content. Third line is added.
❮ PHP Filesystem Reference