PHP inserts a string in the last 140 bytes of the file, but the file pointer is always at the end of the file

Source: Internet
Author: User

  
   ';$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 when written, or written at the end of the file, if this is wrong, why? What is the rational 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 when written, or written at the end of the file, if this is wrong, why? What is the rational idea?

The file is opened in a way that is used r+ .

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

'r'     只读方式打开,将文件指针指向文件头。'r+'    读写方式打开,将文件指针指向文件头。'w'     写入方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。'w+'    读写方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。'a'     写入方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建之。'a+'    读写方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建之。

About fseek Some of the cases.
http://php.net/manual/zh/function.fseek.php

Note:如果使用附加模试(a 或 a+),任何写入文件数据都会被附加上去,而文件的位置将会被忽略,调用 fseek() 的结果尚未定义。

The resulting effect is to write the specified character at the specified position without reaching the insertion effect.
To achieve the effect of the insert, you need to jump to the specified location, get everything behind it, then write what 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.