PHP common file operation function and simple example analysis _php example

Source: Internet
Author: User
Tags fread

PHP most commonly used file operation is read and write, today is mainly about reading and writing functions, and do a page access count function, to record the amount of access to a page.

fopen (): There is no file creation function in PHP, and the fopen () function is used to create and open files, in the form of resource fopen (string filename, string mode)

parameter filename is the file name that opens or creates and opens, parameter mode is an open mode, and the specific mode is as follows:

fread (): PHP can be used to read files, the form of a function is: string fread (resource handle, int length)

Fread () reads up to length bytes from the file pointer handle and stops reading the file when the following conditions are encountered:

When the length byte is read

Reach end of File (EOF)

(for network Flow) 8,192 bytes have been read when a package is available or (after opening the user space stream)

fgets (): PHP is used to read a row of data from a file and point the file pointer to the next line in the form: string fgets (resource handle, int length)

Fgets () reads a line from the pointer handle and returns a string that has a maximum length of (length-1) bytes, and stops reading the file when the following conditions are encountered:

Hit a newline character.

Reach end of File (EOF)

The (length-1) byte has been read

If no length parameter is specified, the default is 1024 bytes.

fwrite (): Used in PHP to write a string to a file, returns the number of characters written when it succeeds, returns false when it fails, and functions as: int fwrite (resource handle, string data, int length)

Fwrite () writes the contents of the string data to the file pointer handle where the parameters are as follows:

If the parameter length is specified, the write is stopped when the length byte is written or the data string is written.

Here we look at an example: Statistics index.php page access and write to count.html file, statistics page of the amount of traffic this trivial, we do not bother the database >_<, the code is as follows:

Put this code in the index.php and add 1 to the index.php,count.html per visit, then we can tell how much the index.php is going through count.html.

PHP Common file operation function is introduced here.

The above PHP commonly used file operation function and simple example analysis is small series to share all the content, hope to give you a reference, also hope that we support cloud habitat community.

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.