PHP Open File fopen function of the use of instructions _php tips

Source: Internet
Author: User
Tags php open file safe mode

1.resource fopen (String $filename, String $mode [, bool $use _include_path [, Resource $zcontext]])
function Function:
fopen () binds the name resource specified by filename to a stream. If filename is "scheme://..." Format, the Search protocol processor (also known as the Encapsulation Protocol) is treated as a url,php to handle this pattern. If the protocol has not yet registered the encapsulation protocol, PHP will issue a message to help check for potential problems in the script and continue with filename as a normal filename.
If PHP thinks that filename specifies a local file, it will attempt to open a stream on the file. The file must be accessible by PHP, so you need to confirm that the file access permission allows the access. If Safe mode is activated or open_basedir, further restrictions are applied.

If PHP believes that filename specifies a registered protocol and that the Protocol is registered as a network url,php will check and confirm that Allow_url_fopen has been activated. If it is turned off, PHP will issue a warning and the fopen call fails.

FileName:Specify the file or URL to open.
mode:Specify the type of access required to the file/stream.
include_path:Optionally, you can set this argument to 1 or true if you also need to retrieve the file in Include_path.
Context :Optionally, specify the context of the file handle, which is a set of options that can be used to fix the behavior of the stream.

Possible values for the mode parameter
Mode description
"R" opens in read-only mode, pointing the file pointer to the file header.
The "r+" read-write mode opens, pointing the file pointer to the file header.
The "W" write mode opens, pointing the file pointer to the file header and truncating the file size to zero. If the file does not exist, try creating it.
The "w+" read-write mode opens, pointing the file pointer to the file header and truncating the file size to zero. If the file does not exist, try creating it.
The "A" write is opened, pointing the file pointer at the end of the file. If the file does not exist, try creating it.
A + read-write mode opens, pointing the file pointer to the end of the file. If the file does not exist, try creating it.
"X"
Creates and opens in writing, pointing the file pointer to the file header. If the file already exists, the fopen () call fails and returns FALSE, and a e_warning level error message is generated. If the file does not exist, try creating it.
This and specifies the o_excl| for the underlying open (2) system call The o_creat tag is equivalent.
This option is supported by PHP 4.3.2 and later versions and can only be used for local files.

"X+"
Create and read-write to open, pointing the file pointer to the file header. If the file already exists, the fopen () call fails and returns FALSE, and a e_warning level error message is generated. If the file does not exist, try creating it.
This and specifies the o_excl| for the underlying open (2) system call The o_creat tag is equivalent.
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 line-ending habits. When you write a text file and want to insert a new row, you need to use the line end symbol that conforms to the operating system. The unix-based system uses \ n as the line end character, and the windows-based system uses \ r \ n as the line end character, and the Macintosh based system uses \ r as the line ending character.

If you use the wrong line-ending symbol when you write to a file, other applications may behave strangely when they open the file.
A text conversion tag (' t ') is provided under Windows to transparently convert \ n to \ r \ n. You can also use ' B ' to force binary mode to be used so that data is not converted. To use these tags, either use ' B ' or ' t ' as the last character of the mode argument.

The default conversion mode relies on SAPI and the PHP version used, so always specify the appropriate markup for easy porting. Use ' t ' in mode if you are manipulating a plain text file and using \ n as the line terminator in the script, but also expect the files to be read by other applications such as Notepad. Use ' B ' in all other cases.

If you do not specify ' B ' tags when manipulating binaries, you may encounter some strange problems, including bad picture files and strange questions about the \ 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.