PHP file read/write operations

Source: Internet
Author: User
PHP provides a series of I/O functions to implement the functions we need, including file system operations and directory operations. The following describes basic file read/write operations: (1) reading files; (2) writing files; (3) appending to files. (Refer to a series of I/O functions provided in php, which can easily implement the functions we need, including file system operations and directory operations. The following describes basic file read/write operations: (1) reading files; (2) writing files; (3) appending to files.

(Refer to the file system function library http://www.phpe.net/manual/ref.filesystem.php beyond php)

Read files:

 

Write a file: (the original content will be overwritten)

 

Append the content to the end of the file: (If the file does not exist, try to create it .)

 

Description

Resource fopen (string filename, string mode [, int use_include_path [, resource zcontext])

Fopen () binds the name resource specified by filename to a stream to open the file and obtain the start handle of the file.

The mode parameter specifies the access type required for the stream. It can be the following:

Table 1. list of possible mode values in fopen ()

Mode description

'R' is opened in read-only mode. it directs the file pointer to the file header.

Open in 'R + 'read/write mode and point the file pointer to the file header.

Open in 'W' writing 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.

Open the 'A' write mode and point the file pointer to the end of the file. If the file does not exist, try to create it.

Open in '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' is created and opened in writing mode. it directs 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 + 'is created and opened in read/write mode. the file pointer points 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.

String fread (int handle, int length)

Fread () reads a maximum of length bytes from the file pointer handle.

This function reads the length of several bytes, or when it reaches the EOF,

Or (for network streams) when a package is available, it will stop reading files, depending on the first situation.

Bool fclose (resource handle)

Fclose -- close an opened file pointer

Int fwrite (resource handle, string [, int length])

Fwrite () writes the string content to the handle of the file pointer. If length is specified, the write will stop after the length byte is written or the string is written.

Fwrite () returns the number of written characters. If an error occurs, FALSE is returned.


Address:

Reprinted at will, but please attach the article address :-)

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.