Php instructions for opening the file fopen function _ PHP Tutorial

Source: Internet
Author: User
Php instructions for opening the file fopen function. 1. resourcefopen (string $ filename, string $ mode [, bool $ use_include_path [, resource $ zcontext]) function: fopen () binds the name resource specified by filename to a stream. For example, 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 \ n as the row end character, Windows-based systems use \ r \ n as the row end character, and Macintosh-based systems use \ r as the row end 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 \ n is used as the row Terminator in the script, but it is expected that these files can be read by other applications such as Notepad, then, use 't' 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.

Http://www.bkjia.com/PHPjc/328060.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/328060.htmlTechArticle1.resource fopen (string $ filename, string $ mode [, bool $ use_include_path [, resource $ zcontext]) function: fopen () bind the name resource specified by filename to a stream. Such...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.