Home > Article > Backend Development > How to use PHP fopen?
How to use PHP fopen?
The function of fopen function in PHP is to open a file or URL. Its syntax is "fopen($filename,$mode,$use_include_path,$context)", and the return value is a file pointer resource. If If the file fails to be opened, FALSE will be returned.
PHP fopen parameters
filename
If filename is "scheme://..." format, is treated as a URL, and PHP will search for a protocol handler (also called a wrapper protocol) to handle this pattern. If the wrapper protocol has not been registered for the protocol, PHP will emit a message to help check for potential problems in the script and continue execution of filename as if it were a normal filename.
If PHP thinks that filename specifies a local file, it will try to open a stream on the file. The file must be accessible to PHP, so you need to confirm that the file access permissions allow this access. If safe mode or open_basedir is activated further restrictions apply.
If PHP believes that filename specifies a registered protocol, and the protocol is registered as a network URL, PHP will check and confirm that allow_url_fopen has been activated. If it is closed, PHP will issue a warning and the call to fopen will fail.
For a list of supported protocols, see Supported Protocols and Encapsulation Protocols. Some protocols (also called wrappers) support context and/or php.ini options. See the corresponding page for which options can be set (e.g. the user_agent value for the http wrapper in php.ini).
mode
The mode parameter specifies the type of access required to the stream. Can be the following:
mode |
Description |
---|---|
##'r' | Open in read-only mode and point the file pointer to the file header.|
'r ' | Open in read-write mode and point the file pointer to the file header.|
'w' | Open writing mode, point the file pointer to the file header and truncate the file size to zero. If the file does not exist, try to create it.|
'w ' | Open in read-write mode, point the file pointer to the file header and truncate the file size to zero. If the file does not exist, try to create it.|
'a' | Open writing mode and point the file pointer to the end of the file. If the file does not exist, try to create it.|
'a ' | Open in 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 for writing, pointing the file pointer to the file header. If the file already exists, the call
fopen() fails and returns FALSE , and generates a E_WARNING level error message. If the file does not exist, try to create it. This and give
The underlying open(2) system call specifies the O_EXCL|O_CREAT flags which are equivalent.
|
'x ' | is created and opened in read-write mode, and other behaviors are the same as'x'. |
'c' | Open the file for writing only. If the file does not exist, the file is created. If it exists, it will neither be truncated (as opposed to "w") nor will the call to this function fail (as is the case with "x"). The file pointer is at the beginning of the file. This may be useful if you need to acquire an advisory lock before trying to modify the file (see flock()), as using "w" may truncate the file before acquiring the lock (if truncation is required, you can use ftruncate after requesting the lock ()).|
'c ' | Open the file for reading and writing; otherwise it behaves the same as 'c'.
Recommended tutorial: "PHP"
The above is the detailed content of How to use PHP fopen?. For more information, please follow other related articles on the PHP Chinese website!