fopen()
fopen() — Opens file or URL.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
fopen() binds a named resource, specified by filename, to a stream.
Signature
fopen(string $filename, string $mode, bool $use_include_path = false, ?resource $context = null): resource|false
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $filename | string | If filename is of the form "scheme://...", it is assumed to be a URL and PHP will search for a protocol handler (also known as a wrapper) for that scheme. If no wrappers for that protocol are registered, PHP will emit a notice to help you track potential problems in your script and then continue as though filename specifies a regular file. |
| $mode | string | The mode parameter specifies the type of access you require to the stream. It may be any of the following: A list of possible modes for fopen() using mode mode Description 'r' Open for reading only; place the file pointer at the beginning of the file. 'r+' Open for reading and writing; place the file pointer at the beginning of the file. 'w' Open for writing only; place the file pointer at the beginning of the file ... |
| $use_include_path | bool | The optional third use_include_path parameter can be set to true if you want to search for the file in the include_path, too. |
| $context | ?resource | A context stream resource. |
Return value
Returns a file pointer resource on success, or false on failure
Example
A runnable snippet:
$handle = fopen("/home/rasmus/file.txt", "r"); $handle = fopen("/home/rasmus/file.gif", "wb"); $handle = fopen("http://www.example.com/", "r"); $handle = fopen("ftp://user:password@example.com/somefile.txt", "w");
Changelog
| Version | Change |
|---|---|
| 7.0.16, 7.1.2 | The 'e' option was added. |
See also
fclose(), fgets(), fread(), fwrite(), fsockopen(), file(), file_exists(), is_readable(), stream_set_timeout(), popen(), stream_context_create(), umask()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page fopen() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.