Home >Backend Development >PHP Problem >How to use PHP fopen?

How to use PHP fopen?

Guanhui
GuanhuiOriginal
2020-06-24 13:40:202475browse

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:

Open in read-only mode and point the file pointer to the file header. Open in read-write mode and point the file pointer to the file header. 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. 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. Open writing mode and point the file pointer to the end of the file. If the file does not exist, try to create it. 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. Create and open for writing, pointing the file pointer to the file header. If the file already exists, the call is created and opened in read-write mode, and other behaviors are the same as 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 ()). Open the file for reading and writing; otherwise it behaves the same as 'c'.
List of possible values ​​for mode in fopen()
mode Description
##'r'
'r '
'w'
'w '
'a'
'a '
'x' 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 ' 'x'.
'c'
'c '
use_include_path

If you also need to search for files in include_path, you can set the optional third parameter use_include_path to '1' or TRUE.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What is PHP cURL?Next article:What is PHP cURL?