How does PHP open or close a file ?,

Source: Internet
Author: User

How does PHP open or close a file ?,

What is File Processing?

File processing includes reading, disabling, and rewriting. To master file processing, you need to clarify your ideas and master the key steps and common functions of File Processing, so you can use them freely! If you are interested, go to the PHP File Processing topic.

For example, it takes three steps to access a file: open the file, read and write the file, and close the file. Other operations on a file include either reading and writing files (such as displaying and writing content) or attributes of the file (such as file traversal and renaming ). This article will introduce common file processing technologies in detail.

This article introducesOpen/close a fileTo open the file and close the file. Use the fopen () function and fclose () function. You should be very careful when opening the file, because it is possible to delete all the file content accidentally.

I. Open a file

When operating a file, the first thing we need to do is to open the file, which is the first step for data access. Use the fopen () function in PHP to open the file. the syntax of the fopen () function is as follows:

1 resource fopen ( string $filename , string $mode [, bool $use_include_path = false [, resource $context]] )

 

The filename parameter is the name of the file containing the path. It can be a relative or absolute path. If there is no prefix side indicating that the local file is opened, the mode parameter is used to open the file. The values can be as follows:

Mode Mode name Description
R Read-Only Read mode-read. The file pointer is located at the beginning of the file.
R + Read/write Read/write mode-read/write. The file pointer is located at the beginning of the file. Writing to the end of the existing file will overwrite the original
W Write only Write mode-Write the file, and the file Pointer Points to the header file. If the file exists, all the file content will be deleted; otherwise, the function will create the file
W + Read/write Read/write mode-read/write. The file Pointer Points to the header file. If the file exists, all the file content will be deleted; otherwise, the function will create the file
X Write with caution In write mode, open the file and start writing from the file header. If the file already exists, the file will not be opened. If the function returns false, PHP will generate a warning.
X + Write with caution In read/write mode, open the file and start writing from the file header. If the file already exists, the file will not be opened. If the function returns false, PHP will generate a warning.
A Append In append mode, open the file. The file Pointer Points to the end file. If the file already contains content, it will be appended from the end of the file. If the file does not exist, the function will create the file.
A + Append In append mode, open the file. The file Pointer Points to the header file. If the file already contains content, it will be appended or read from the end of the file. If the file does not exist, the function will create the file.
B Binary Binary Mode-used to connect to other modes. If the file system can distinguish between binary files and text files, it may be used. Windows can be distinguished; UNIX is not distinguished. This option is recommended for maximum portability. It is the default mode.
T Text It is used in combination with other modes. This mode is only an option in Windows.

The third parameter use_include_path is optional. ini, such as F: \ AppServ \ www \ mess. php. If you want the server to open the specified file in this path, you can set the value to 1 or true.

Ii. Close files

Close the file after the operation, otherwise it may cause errors. Use the fclose () function in PHP to close the file. The syntax format of this function is as follows;

1 bool fclose ( resource $handle )

 

This function disables the file to which the handle parameter points. If it succeeds, true is returned; otherwise, false is returned. The file pointer must be valid and be a file successfully opened through the fopen () function.

For details, see the following sample code:

123456 <?phpheader("Content-Type:text/html; charset=utf-8");$f_open fopen("../file.txt","rb"); // Open the file.................                      // Operations on filesfclose($f_open)                       // Close the file after the operation is complete?>

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.