Use of the fopen function in php to open files

Source: Internet
Author: User
This article provides a detailed analysis of the use of the fopen function in php. For more information, see

This article provides a detailed analysis of the use of the fopen function in php. For more information, see

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 be opened.
Mode: Specifies the access type of the file/stream.
Include_path: Optional. If you need to search for a file in include_path, you can set this parameter to 1 or TRUE.
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.
"R +" is opened in read/write mode, Vm, 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, Vm, and point 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, which is free of Record Filing space. Therefore, to facilitate migration, you must 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.

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.