Content operation of the file

Source: Internet
Author: User
Tags rewind

<?php
Open, Append Way
$fp = fopen ("Demo.txt", "a");

Rewind ($FP);
Echo Ftell ($fp). " <br> ";
Append by default at end of file
Fwrite ($fp, "Hello world\n");
Shut down
Fclose ($FP);


Open it
$fp = fopen ("Demo.txt", "R");

Echo Ftell ($fp). " <br> ";
Fseek ($FP, 4);//Move the pointer back 4 positions
Echo Ftell ($fp). " <br> ";//returns the current pointer position
Echo fread ($FP, 11). " <br> ";
Echo Ftell ($fp). " <br> ";
Fseek ($fp, -3, seek_end);//move 3 bytes from the last, and newline is also a byte
Echo Fread ($FP, 3);
Rewind ($FP);//directly point the file pointer to the beginning
Echo Ftell ($fp). " <br> ";
Shut down
Fclose ($FP);


Open it
$fp = fopen ("Demo.txt", "R");

while (!feof ($fp)) {
Read the file, read n characters at a time
Echo fread ($FP, 1024);
}
Shut down
Fclose ($FP);


Open it
$fp = fopen ("Demo.txt", "R");

If the file is faulted or the end of the file returns True
while (!feof ($fp)) {
Read a file, read one character at a time
echo fgetc ($FP);
}
Shut down
Fclose ($FP);


Open it
$fp = fopen ("Demo.txt", "R");

while (!feof ($fp)) {
Read a file, read one line at a time
Echo fgets ($fp). " <br> ";
}
Shut down
Fclose ($FP);

Content operation of the file

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.