PHP fopen() function
Definition and Usage
The fopen() function opens a file or URL.
If the operation fails, this function returns FALSE.
Syntax
fopen(filename,mode,include_path,context)
Parameter | Description |
---|---|
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 listed in the table below. |
include_path | Optional. If you also need to search for files in the include_path, you can set this parameter to 1 or TRUE. |
context | Optional. Specifies the environment of the file handle. Context is a set of options that can modify the behavior of the stream. |
Possible values of the mode parameter
mode | indicates |
---|---|
"r" | Open the file in read-only mode and move the file pointer to the beginning of the file. |
"r+" | Open the file in read/write mode and move the file pointer to the beginning of the file. |
"w" | Open the file in write mode and move the file pointer to the beginning of the file, and truncate the file size to zero. If the file does not exist, it attempts to create it. |
"w+" | Open the file in read/write mode and move the file pointer to the beginning of the file, and truncate the file size to zero. If the file does not exist, it attempts to create it. |
"a" | Open the file in write mode and move the file pointer to the end of the file. If the file does not exist, it attempts to create it. |
"a+" | Open the file in read/write mode and move the file pointer to the end of the file. If the file does not exist, it attempts to create it. |
"x" |
Open the file in write mode and move the file pointer to the beginning of the file. If the file already exists, the fopen() call fails and returns FALSE, generating an E_WARNING level error message. If the file does not exist, it attempts to create it. is equivalent to specifying the O_EXCL|O_CREAT flag for 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, and point the file pointer to the beginning of the file. If the file already exists, the fopen() call will fail and return FALSE, and an E_WARNING level error message will be generated. If the file does not exist, it will attempt to create it. is equivalent to specifying the O_EXCL|O_CREAT flag for 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. |
indicates
fopen() will filename binds the specified name resource to a stream. If filename is "scheme://...If the format is in the form of "", it is treated as a URL, and PHP will search for a protocol handler (also known as an encapsulation protocol) to handle this mode. If the protocol has not been registered as an encapsulation protocol, PHP will issue a message to help check for potential issues in the script and filename as a regular filename to proceed.
if PHP thinks filename specifies a local file, it will try to open a stream on that file. The file must be accessible to PHP, so it is necessary to confirm that file access permissions allow this access. Further restrictions will be applied if safe mode or open_basedir is activated.
if PHP thinks filename specifies a registered protocol, which is registered as a network URL, and PHP will check and confirm that allow_url_fopen has been activated. If it is disabled, PHP will issue a warning, and the fopen call will fail.
for context support was added in PHP 5.0.0.
Tips and comments
Note:Different operating system families have different line ending habits. When writing a text file and inserting a new line, the line ending symbol that is consistent with the operating system needs to be used. Unix-based systems use \n as the line ending character, Windows-based systems use \r\n as the line ending character, and Macintosh-based systems use \r as the line ending character. If the wrong line ending symbol is used when writing to a file, other applications may behave strangely when opening these files.
Windows provides a text conversion marker ("t") that can transparently convert \n to \r\n. Correspondingly, "b" can be used to force binary mode, which will not convert data. To use these markers, either use "b" or "t" as the last character of the mode parameter.
The default conversion mode depends on the SAPI and the version of PHP being used, so for portability, it is encouraged to always specify the appropriate flag. If you are operating plain text files and using \n as the line terminator in the script, but still expect these files to be readable by other applications such as Notepad, use "t" in the mode. In all other cases, use "b".
Without specifying the "b" flag when operating binary files, you may encounter some strange issues, including corrupted image files and strange issues related to \r\n characters.
Note:For portability, it is strongly recommended to always use the "b" flag when opening files with fopen().
Note:Again, for portability, it is strongly recommended that you rewrite the code that depends on the "t" mode to use the correct line terminator and change it to "b" mode.
Examples
<?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"); ?>