fopen()
function opens a file or URL.
If the opening fails, this function returns FALSE.
<?php $file = fopen("test.txt","r"); $file = fopen("/home/test/test.txt","r"); $file = fopen("/home/test/test.gif","wb"); $file = fopen("http://www.example.com/","r"); $file = fopen("ftp://user:password@example.com/test.txt","w"); ?>
fopen ( filename , mode , include_path , context )
parameter | describe |
---|---|
filename | Required. Specifies the file or URL to be opened. |
mode | Required. Specifies the type of access required to the file/stream. Possible values are shown in the table below. |
include_path | Optional. If you also need to retrieve files in include_path, you can set this parameter to 1 or TRUE. |
context | Optional. Specifies the environment for file handles. Context is a set of options that can modify the behavior of a stream. |
mode | illustrate |
---|---|
"r" | Open read-only, pointing the file pointer to the file header. |
"r+" | Read and write mode is turned on, pointing the file pointer to the file header. |
"w" | Write mode opens, point the file pointer to the file header and cuts the file size to zero. If the file does not exist, try to create it. |
"w+" | Read and write mode is turned on, pointing the file pointer to the file header and severing the file size to zero. If the file does not exist, try to create it. |
"a" | Write mode opens, pointing the file pointer to the end of the file. If the file does not exist, try to create it. |
"a+" | Read and write mode is turned on, pointing the file pointer to the end of the file. If the file does not exist, try to create it. |
"x" |
Create and open as write, pointing the file pointer to the file header. If the file already exists, the fopen() call fails and returns FALSE, and an E_WARNING-level error message is generated. If the file does not exist, try to create it. This is equivalent to specifying the O_EXCL|O_CREAT tag to the underlying open(2) system call. This option is supported by PHP 4.3.2 and later versions and can only be used for local files. |
"x+" |
Create and open in read-write mode, pointing the file pointer to the file header. If the file already exists, the fopen() call fails and returns FALSE, and an E_WARNING-level error message is generated. If the file does not exist, try to create it. This is equivalent to specifying the O_EXCL|O_CREAT tag to the underlying open(2) system call. This option is supported by PHP 4.3.2 and later versions and can only be used for local files. |
fopen()
binds the name resource specified by filename to a stream. If the filename is in the format of " scheme://... ", it is treated as a URL, and PHP will search the protocol processor (also known as the encapsulation protocol) to handle this mode. If the protocol has not registered the encapsulation protocol, PHP will send a message to help check for potential problems in the script and continue executing filename as a normal filename.
If PHP thinks that filename specifies a local file, it will attempt to open a stream on that file. The file must be accessible by PHP, so you need to confirm that file access permission allows this access. If Safe Mode is activated or open_basedir is activated, further restrictions will be applied.
If PHP believes that filename specifies a registered protocol and that protocol is registered as a network URL, PHP will check and confirm that allow_url_fopen has been activated. If closed, PHP will issue a warning and fopen's call fails.
Support for context is added by PHP 5.0.0.