This article provides a detailed analysis of the use of the fopen function in php. For more information, see section 1. resource fopen (string $ filename, string $ mode [, bool $ use_include_path [, resource $ zcontext])
Function:
Fopen () binds the name resource specified by filename to a stream. If filename is in the format of "scheme: //...", it is treated as a URL. PHP will use the search protocol processor (also known as the Encapsulation Protocol) to process this mode. If the protocol has not yet registered the Encapsulation Protocol, PHP will send a message to help check the potential problems in the script and continue executing the filename as a normal file name.
If PHP considers filename to be a local file, it will try to open a stream on the file. This file must be accessible by PHP. Therefore, you need to confirm that the file access permission permits this access. If the security mode or open_basedir is activated, further restrictions are applied.
If PHP considers filename to be a registered protocol registered as a web URL, PHP checks and confirms that allow_url_fopen is activated. If it is disabled, PHP will issue a warning, while the fopen call will fail.
Filename:Specifies the file or URL to open.
Mode:Specifies the access type of the file/stream.
Include_path:Optional. you can set this parameter to 1 or TRUE if you also need to retrieve files in include_path.
Context:Optional. specifies the file handle environment. Context is a set of options for modifying the behavior of the stream.
Possible value of the mode parameter
Mode description
Open the file in read-only mode and point the file pointer to the file header.
Open the "r +" read/write mode and point the file pointer to the file header.
Open the "w" write mode, point the file pointer to the file header, and cut the file size to zero. If the file does not exist, try to create it.
Open in "w +" read/write mode, point the file pointer to the file header, and cut the file size to zero. If the file does not exist, try to create it.
"A" is opened in writing mode, pointing the file pointer to the end of the file. If the file does not exist, try to create it.
Open the "a +" read/write mode and point the file pointer to the end of the file. If the file does not exist, try to create it.
"X"
Create and open the file in writing mode, and point the file pointer to the file header. If the file already exists, fopen () fails to be called, returns FALSE, and generates an E_WARNING-level error message. If the file does not exist, try to create it.
This is equivalent to specifying the O_EXCL | O_CREAT mark 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 the file in read/write mode, and point the file pointer to the file header. If the file already exists, fopen () fails to be called, returns FALSE, and generates an E_WARNING-level error message. If the file does not exist, try to create it.
This is equivalent to specifying the O_EXCL | O_CREAT mark 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.
Note:
Different operating system families have different row termination habits. When you write a text file and want to insert a new row, you must use the line termination symbol that complies with the operating system. Unix-based systems use the row ending character, Windows-based systems use \ r \ n as the row ending character, and Macintosh-based systems use \ r as the row ending character.
If an incorrect row end symbol is used when writing files, other applications may encounter a strange behavior when opening these files.
In Windows, a text conversion mark ('t') is provided to transparently convert \ n to \ r \ n. You can also use 'B' to force the binary mode to avoid data conversion. To use these tags, either 'B' or 'T' is used as the last character of the mode parameter.
The default conversion mode depends on SAPI and the PHP version used. therefore, to facilitate migration, you should always specify the appropriate tag. If a plain text file is operated and used as a line Terminator in the script, but it is expected that these files can be read by other applications such as Notepad, then 't' is used in the mode '. Use 'B' in all other cases '.
If the 'B' flag is not specified when operating the binary file, some strange problems may occur, including bad image files and strange problems about \ r \ n characters.