In php, a string is inserted at the last 140 bytes of the file, but the file pointer is always at the end of the file.

Source: Internet
Author: User
{Code ...} I want to ask why I have used fseek to change the file pointer to the last 140 bytes, but it is still written at the end of the file. If so, why? What is a reasonable idea?

  ';$handle = fopen("D:/wamp/www/liuyanban.html","a"); fseek($handle, -140,SEEK_END); fwrite($handle, "ok"); fclose($handle);?>

I want to ask why I have used fseek to change the file pointer to the last 140 bytes, but it is still written at the end of the file. If so, why? What is a reasonable idea?

Reply content:

  ';$handle = fopen("D:/wamp/www/liuyanban.html","a"); fseek($handle, -140,SEEK_END); fwrite($handle, "ok"); fclose($handle);?>

I want to ask why I have used fseek to change the file pointer to the last 140 bytes, but it is still written at the end of the file. If so, why? What is a reasonable idea?

File Opening Methodr+.

Http://php.net/manual/zh/function.fopen.php

'R' is opened in read-only mode. It directs the file pointer to the file header. Open in 'r + 'read/write mode and point the file pointer to the file header. Open in 'W' writing mode, point the file pointer to the file header, and cut the file size to zero. If the file does not exist, try to create it. Open in 'W + 'read/write mode, point the file pointer to the file header, and cut the file size to zero. If the file does not exist, try to create it. Open the 'A' write mode and point the file pointer to the end of the file. If the file does not exist, try to create it. Open in 'a + 'read/write mode and point the file pointer to the end of the file. If the file does not exist, try to create it.

Aboutfseek.
Http://php.net/manual/zh/function.fseek.php

Note: If you use the attached mode (a or a +), any data written to the file will be appended, and the file location will be ignored. Call fseek () the result is not defined yet.

The result is that the specified character is written at the specified position, but the insert effect is not achieved.
To achieve the insert effect, you need to first jump to the specified location, get all the content behind it, then write the content you want to write, and then write the original content.

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.