PHP common file operation function and simple example analysis _php instance

Source: Internet
Author: User
Tags fread
PHP most commonly used file operation is read and write, today, mainly on the reading and writing functions, and do a page access to the Count function, to record the amount of access to a page.

fopen (): There is no file creation function in PHP, both the creation and opening of the file are using the fopen () function, in the form of the function: Resource fopen (string filename, string mode)

The parameter filename is the file name opened or created and opened, and the parameter mode is the open mode, as follows:

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

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

When you have finished reading the length of bytes

Reach end of File (EOF)

(for network streams) 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 of the function: string fgets (resource handle, int length)

Fgets () reads a row from the pointer handle and returns a string of up to (length-1) bytes, which stops reading the file when the following conditions are encountered:

Encounter line break

Reach end of File (EOF)

(length-1) bytes have been read

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

fwrite (): Used in PHP to write a string to a file, returns the number of characters written on success, false on failure, and the function as: int fwrite (resource handle, string data, int length)

Fwrite () writes the contents of the string data to the file pointer handle at the following parameters:

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

Let's take a look at an example: Statistics index.php page access and write to count.html file, Statistics page access volume This little thing, we do not bother the database >_<, the code is as follows:

Put this code in index.php, every visit to index.php,count.html will add 1, then through count.html we can know index.php total traffic is how much.

PHP Common file operation function is introduced here.

The above PHP commonly used file operation function and simple example analysis is the small part of the whole content to share to everyone, I hope to give you a reference, but also hope that we support the script home.

  • 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.