fopen
fopen
Opens file or URL
**fopen** string $filename string $mode bool $use_include_path $context
binds a named resource, specified by
, to a stream.
fopen``filename
filename
If 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
specifies a regular file.
filename``filename
If PHP has decided that specifies
a local file, then it will try to open a stream on that file.
The file must be accessible to PHP, so you need to ensure that
the file access permissions allow this access.
If you have enabled
further
restrictions may apply.
`filename`open_basedir
If PHP has decided that specifies
a registered protocol, and that protocol is registered as a
network URL, PHP will check to make sure that
is
enabled. If it is switched off, PHP will emit a warning and
the call will fail.
`filename`allow_url_fopen`fopen`
Opmerking: > The list of supported protocols can be found in . Some protocols (also referred to as ) support and/or php.ini options. Refer to the specific page for the protocol in use for a list of options which can be set. (e.g. php.ini value used by the wrapper).
wrappers``context``user_agent``http
On the Windows platform, be careful to escape any backslashes
used in the path to the file, or use forward slashes.
```php
`mode`
The parameter specifies the type of access
you require to the stream. It may be any of the following:
`mode`
> **Opmerking:** > Different operating system families have different line-ending
> conventions. When you write a text file and want to insert a line
> break, you need to use the correct line-ending character(s) for your
> operating system. Unix based systems use as the
> line ending character, Windows based systems use
> as the line ending characters and Macintosh based systems (Mac OS Classic) used
> as the line ending character.
> `\n``\r\n``\r`
>
> If you use the wrong line ending characters when writing your files, you
> might find that other applications that open those files will "look
> funny".
>
>
> Windows offers a text-mode translation flag ()
> which will transparently translate to
> when working with the file. In contrast, you
> can also use to force binary mode, which will not
> translate your data. To use these flags, specify either
> or as the last character
> of the parameter.
> `'t'``\n``\r\n``'b'``'b'``'t'``mode`
>
>
> The default translation mode is .
> You can use the
> mode if you are working with plain-text files and you use
> to delimit your line endings in your script, but
> expect your files to be readable with applications such as old versions of notepad. You
> should use the in all other cases.
> `'b'``'t'``\n``'b'`
>
>
> If you specify the 't' flag when working with binary files, you
> may experience strange problems with your data, including broken image
> files and strange problems with characters.
> `\r\n`
> **Opmerking:** > For portability, it is also strongly recommended that
> you re-write code that uses or relies upon the
> mode so that it uses the correct line endings and
> mode instead.
> `'t'``'b'`
> **Opmerking:** > The is ignored for ,
> , ,
> , and
> stream wrappers.
> `mode`
`use_include_path`
The optional third parameter
can be set to true if you want to search for the file in the
, too.
`use_include_path`include_path
`context`
Returns a file pointer resource on success,
return.falseforfailure
**Voorbeeld: examples**
```php
<?php
$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");
?>
Opmerking: > If you are experiencing problems with reading and writing to files and you're using the server module version of PHP, remember to make sure that the files and directories you're using are accessible to the server process.
Opmerking: > This function may also succeed when is a directory. If you are unsure whether is a file or a directory, you may need to use the function before calling .
filename``filename``is_dir``fopen
fclose``fgets``fread``fwrite``fsockopen``file``file_exists``is_readable``stream_set_timeout``popen``stream_context_create``umask``SplFileObject