Definition :
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.
fopen(filename, mode, include_path, context)
Parameter | Description |
---|---|
filename | Required. Specifies the file or URL to open |
mode | Required. Specifies the type of access you require to the file/stream.
Possible values:
|
include_path | Optional. Set this parameter to '1' if you want to search for the file in the include_path (in php.ini) as well |
context | Optional. Specifies the context of the file handle. Context is a set of options that can modify the behavior of a stream |
Lets assume that we have a file called demo.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.
Output :
This is a test file.
It contains dummy content.