Php file processing advanced application-file pointer

Source: Internet
Author: User
PHP can locate and query file pointers to quickly query required information. file pointer functions include rewind (), fseek (), feof (), and ftell. Php file processing advanced application-file pointer

PHP can locate and query file pointers to quickly query required information. file pointer functions include rewind (), fseek (), feof (), and ftell.

In the previous article "advanced application for PHP file processing-remote file access and file locking", we introduced the remote access to files and how to lock files, let's take a look. I will not talk about it much here. today we will mainly talk about functions in file pointers!

I. rewind () function

This function sets the pointer of the handle file to the beginning of the file stream. the syntax format of this function is as follows:

bool rewind ( resource $handle )

Note:

If you open the file and append ("a") mode, any data written to the file will always be appended to the end of the existing content of the file, no matter where the file pointer is located!

II. fseek () function

The fseek () function locates the file pointer. the syntax format of this function is as follows:

int fseek ( resource $handle , int $offset [, int $whence = SEEK_SET ] )
  • The handle parameter is the file to be opened.

  • Offset is the pointer position or offset relative to the whence parameter. it can be a negative value.

  • Whence values include the following:

1. SEEK_SET, with the position equal to the offset byte.

2. SEEK_CUR. the position is equal to the current position plus the offset byte.

3. SEEK_END, with the position equal to the end of the file plus the offset byte.

If the whence parameter is ignored, the default value is SEEK_SET.

3: feof () function

This function determines whether the file pointer is at the end of the file. the syntax of the function is as follows:

bool feof ( resource $handle )

If the pointer ends at the end of the file, true is returned; otherwise, false is returned.

4. ftell () function

The ftell () function returns the position of the current pointer. the syntax format of this function is as follows:

int ftell ( resource $handle )

The following code uses the above four pointer functions to output the content in the file:

 "; // Total number of output bytes $ fopen = fopen ($ filename," rb "); // open the file echo" initial pointer position :". ftell ($ fopen )."
"; // Output pointer position fseek ($ fopen, 33); // brake pointer echo" pointer position after using the fseek () function: ". ftell ($ fopen )."
"; // Output the pointer position after braking echo" output content behind the current pointer: ". fgets ($ fopen )."
"; // Output the content from the current pointer to the end of the line if (feof ($ fopen) {// Determine whether the pointer points to the end of the file echo" the current pointer points to the end of the file :". ftell ($ fopen )."
"; // If it points to the end of the file, the output pointer position is rewind ($ fopen); // The position of the pointer after the rewind () function is used echo: ". ftell ($ fopen )."
"; // View the position of the pointer after the rewind () function is used echo" output content of the first 33 bytes :". fgets ($ fopen, 33); // output the content of the first 33 bytes fclose ($ fopen); // close the file} else {echo "the file does not exist" ;}}?>

Output result:

The advanced application of php file processing is finished here. next we will introduce php file upload. for details, see php file upload-configure php. inn FILES and predefined variables $ _ FILES!

The above is the details of the advanced application of PHP file processing-file pointer. For more information, see other related articles in the first PHP 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.