Instructions for using PHP to open file fopen function

Source: Internet
Author: User
Tags file size safe mode versions
This article is a PHP in the open file fopen function of the use of a detailed analysis of the introduction, the need for friends to reference the  

1.resource  fopen (string  $filename, String $mode [, bool $use _include_path [, Resource $zcontext]]) br> the 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 believes that filename specifies a local file, it will attempt to open a stream on that 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: To specify the file or URL to open.
Mode: sets the type of access required to the file/stream.
include_path: Optional, you can set this argument to 1 or true if you also need to retrieve the file in Include_path.
Context: Optionally, an environment that prescribes 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"   read-only to open, 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, points the file pointer to the file header, and truncates the file size to zero. If the file does not exist, try creating it.
A   write is opened, pointing the file pointer to the end of the file. If the file does not exist, try creating it. The
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"  
is created and opened as a write, 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 RN as the line end character, and the Macintosh based system uses R as the line end 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 RN. 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. If you are manipulating a plain text file and using n as a line terminator in your script, but expect the files to be read by other applications such as Notepad, use ' t ' in mode. Use ' B ' in all other cases.

If you do not specify the ' B ' tag when manipulating binary files, you may encounter some strange problems, including bad picture files and strange questions about the RN character.

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.