PHP mobile file pointer ftell (), fseek (), rewind () function summary

Source: Internet
Author: User
Tags rewind
This article mainly introduces the PHP mobile file pointer ftell (), fseek (), rewind () function summary. This article first explains their functions, and then provides specific examples, for more information about how to read and write a file, you may need to jump to the file, read data from the same location, and write data to different locations. For example, if you use a file simulation database to save data, you need to move the file pointer. The pointer position is measured by the number of bytes starting from the file header. by default, when a file is opened in different modes, the file pointer is usually at the beginning or end of the file. you can use ftell () the fseek () and rewind () functions operate on file pointers. their prototype is as follows:

The code is as follows:


Int ftell (resource handle) // returns the current position of the file pointer
Int fseek (resource hanlde, int offset [, int whence]) // move the file pointer to the specified position
Bool rewind (resource handle) // move the file pointer to the beginning of the file

When using these functions, you must provide a valid file pointer opened with the fopen () function. The ftell () function gets the offset from the current position of the file pointer in the specified resource. The rewind () function moves the file pointer back to the beginning of the specified resource. the fseek () function () the function moves the pointer to the position specified by the second offset parameter. if the third optional parameter whence is not provided, the position is set to the offset byte starting from the file. Otherwise, the third parameter whence can be set to three possible values, which will affect the pointer position.

★SEEK_CUR: Set the pointer position to add the offset byte provided by the second parameter to the current position.
★SEEK_END: Set the pointer position to EOF plus the offset byte. Here, offset must be set to a negative value.
★SEEK_SET: Set the pointer position to the offset byte. This is the same as ignoring the whence parameter.

If the fseek () function is successfully executed, 0 is returned, and-1 is returned if the function fails. if the file is opened in the append mode "a" or "a +", any data written to the file will be appended to the file, regardless of the file pointer location. The code is as follows:

The code is as follows:


<? Php
$ Fp = fopen('data.txt ', 'r') or die ("file opening failed ");

Echo ftell ($ fp )."
"; // Output the default position of the pointer to the opened file. the pointer is 0 at the beginning of the file.
Echo fread ($ fp, 10 )."
"; // Read the first 10 characters in the file and the pointer position has changed.
Echo ftell ($ fp )."
"; // After reading the first 10 characters of the object, the pointer is moved at the position of 10th bytes.

Fseek ($ fp, 100, SEEK_CUR); // move the pointer to the last 10 bytes
Echo ftell ($ fp); // The file is located at 110 bytes.
Echo fread ($ fp, 10 )."
"; // Read the string between 110 and 120 bytes. after reading, the pointer is at 120 bytes.

Fseek ($ fp,-10, SEEK_END); // move the pointer to the last 10 bytes.
Echo fread ($ fp, 10 )."
"; // The Last 10 characters in the output file

Rewind ($ fp); // move the file pointer to the beginning of the file
Echo ftell ($ fp); // pointer at the beginning of the file, output 0

Fclose ($ fp );
?>

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.