Home  >  Article  >  Backend Development  >  How to use php fopen function

How to use php fopen function

藏色散人
藏色散人Original
2019-05-28 14:23:342996browse

php The fopen function is used to open a file or URL. Its syntax is fopen(filename, mode, include_path, context). The parameter filename is required, which refers to the file or URL to be opened. Mode is required, which refers to the requirement to the file/ The access type of the stream.

How to use php fopen function

#How to use php fopen function?

Definition and usage

fopen() function opens a file or URL.

If the opening fails, this function returns FALSE.

Syntax

fopen(filename,mode,include_path,context)

Parameters

filename required. Specifies the file or URL to open.

mode Required. Specifies the type of access required to this 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 a file handle. Context is a set of options that can modify the behavior of the stream.

Possible values ​​for the mode parameter

mode Description

"r" Open in read-only mode, pointing the file pointer to the file header.

"r " Open in read-write mode and point the file pointer to the file header.

"w" turns on writing mode, points the file pointer to the file header and truncates 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 in 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" is created and opened for writing, pointing the file pointer to the file header. If the file already exists, the fopen() call fails and 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 flag 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 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 flag 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.

Description

fopen() Binds the name resource specified by filename to a stream. If filename is of the form "scheme://...", it is treated as a URL and PHP will search for a protocol handler (also called a wrapper protocol) to handle the scheme. 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.

Support for context was added in PHP 5.0.0.

Tips and Notes

Note: Different operating system families have different line ending conventions. When writing to a text file and want to insert a new line, you need to use operating system-compliant line endings. 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 files are written with incorrect line endings, other applications may behave strangely when opening the files.

Windows provides a text conversion tag ("t") that can transparently convert \n to \r\n. Alternatively, "b" can be used to force binary mode so that the data is not converted. To use these flags, use either "b" or "t" as the last character of the mode argument.

The default conversion mode depends on the SAPI and PHP version used, so for portability it is encouraged to always specify the appropriate tags. If you are working with plain text files and use \n as the line terminator in your script, but you also want the files to be readable by other applications such as Notepad, use "t" in mode. Use "b" in all other cases.

在操作二进制文件时如果没有指定 "b" 标记,可能会碰到一些奇怪的问题,包括坏掉的图片文件以及关于 \r\n 字符的奇怪问题。

注释:为移植性考虑,强烈建议在用 fopen() 打开文件时总是使用 "b" 标记。

注释:再一次,为移植性考虑,强烈建议你重写那些依赖于 "t" 模式的代码使其使用正确的行结束符并改成 "b" 模式。

例子

<?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");
?>

The above is the detailed content of How to use php fopen function. 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