PHP File Operation Example Explained

Source: Internet
Author: User
This article is mainly to share with you PHP file operation examples, file_put_contents (file address, content [, File_append]); File_append represents an append write. There's no third parameter. The contents of the original file are cleared and then written.

File_get_content (file address); Read the contents of the file.

Both of these functions are read and write to the file, but can only read the contents of all files at once, and when the file is too large, the function does not apply. The following sequence of functions can be used to manipulate the file.

fopen (file address, open mode);

How to open in PHP: R (Read) read-only mode.

W (write) write mode. Clears the contents of the file and writes it (automatically creates files that do not exist).

X Replace write. Like W, however, the file cannot be created automatically and will be an error when the file does not exist.

A (append) append write. Add input at the end of the original file. (That is, the file pointer is at the end after opening the file)

r+ read/write mode. When you open a file, the file pointer is placed at the beginning, and unlike R, it can also be written at the pointer position.

w+ read/write mode. Clear the contents of the file and place the pointer over the file header. Both read and write operations can be performed.

A + append read and write mode. The write is always at the end of the file and is limited by the pointer position.

x+ Replace read-write mode. ...................................................................................

Fread (file handle [, length]); reads the specified length of content (in bytes, up to 8192 bytes).

Fgets (file handle, length); reads a string of the specified length (actual read length = specified length-1), except the length, at the end of the line is the function's terminating read operation. Depends on satisfying that condition first. Typically used to read a row of data.

FGETC (file handle); read one byte at a time.

Note: All three read functions manipulate the pointer, and the pointer is moved during the read.

Fwrite (file handle, write content); Writes the content at the specified location, usually indicated by a file pointer.

Fclose (file handle); Close file.

Functions for manipulating pointers:

Ftell (file handle): Gets the position of the pointer.

Fseek (file handle, number of bytes): Sets the position of the pointer.

Other functions:

Filemtime (file address); The time when the file was last modified.

FileSize (file address); File size (bytes).

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.