Common PHP file operation functions and simple instance analysis,

Source: Internet
Author: User

Common PHP file operation functions and simple instance analysis,

The most common file operations in PHP are reading and writing. Today, we will mainly explain how to read and write functions, and create a page access count function to record the access volume of a page.

Fopen ():PHP does not contain file creation functions. fopen () is used to create and open files. The function format is resource fopen (string filename, string mode)

The filename parameter is the name of the file to be opened or created and opened. The mode parameter is the mode to open. The specific mode is as follows:

Fread ():PHP can be used to read files. The function format is string fread (resource handle, int length)

Fread () reads a maximum of length bytes from the handle of the file pointer. It stops reading files in the following situations:

When length is read

Reached the end of the file (EOF)

(For Network streams) when a package is available or (after the user space stream is opened) It has read 8192 bytes

Fgets ():PHP is used to read a row of data from the file and direct the file pointer to the next row. The function is in the form of string fgets (resource handle, int length)

Fgets () reads a row from the pointer handle and returns a string of up to (length-1) bytes. It stops reading files in the following situations:

Line Break

Reached the end of the file (EOF)

Read (length-1) bytes

If length is not specified, the default value is 1024 bytes.

Fwrite ():PHP is used to write a string to a file. The number of written characters is returned when the file is successful. If the file fails, false is returned. The function form is int fwrite (resource handle, string data, int length)

Fwrite () writes the content of string data to the file pointer handle. The parameters are as follows:

If the length parameter is specified, the write will stop after the length bytes are written or the data string is written.

Let's take a look at an example: calculate the volume of content on the index.php page and write it into the count.html file. to count the page access volume, we will not bother with the database >_<. The Code is as follows:

Put this code in index.php. Every time you upload index.php, count.html will add 1. Then, through count.html, we can know the total access volume of index. php.

This section describes common PHP file operation functions.

The above common PHP file operation functions and simple instance analysis are all the content shared by the editor. I hope to give you a reference and support for the help house.

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.