Implementation code for PHP read-write files

Source: Internet
Author: User
    1. $fp = fopen ("Test.txt", "R");
    2. ?>
Copy Code

2.fclose (Close file) syntax: fclose (filepointer) filepointer, the file pointer to be closed. If successful, the Fclose function returns TRUE if the Fclose function returns FALSE if it fails. Example:

    1. $fp = fopen ("Test.txt", "R");
    2. Fclose ($FP);
    3. ?>
Copy Code

3.feof (detects whether the end of file has been reached) syntax: feof (filepointer) Filepointer, the pointer to the file to be detected, must point to a file that was successfully opened without closing. The feof function returns TRUE if the file pointer is to the end of the file or if an error occurs. Example:

    1. $fp = fopen ("Test.txt", "R");
    2. while (! feof ($FP))
    3. {
    4. Echo fgets ($FP). "
      ";
    5. }
    6. Fclose ($FP);
    7. ?>
Copy Code

4.fgets (reads a line from the file pointer) syntax: fgets (filepointer) filepointer, the file pointer to be read. If successful, reads a row from the file and returns a string, FALSE if it fails. Example:

    1. $fp = fopen ("Test.txt", "R");
    2. if ($FP)
    3. {
    4. For ($i =1;! feof ($fp); $i + +)
    5. {
    6. echo "line". $i. ":". Fgets ($FP). "
      ";
    7. }
    8. }
    9. Else
    10. {
    11. echo "Failed to open file";
    12. }
    13. Fclose ($FP);
    14. ?>
Copy Code

Suppose Test.txt's content is: Hello Worldhello cnblogshello Heihaozihello The Everyone page output results in: Row 1:hello World row 2:hello cnblogs line 3:hello H Eihaozi Line 4:hello Everyone

5.fwrite (write file) syntax: fwrite (filepointer,string) filepointer, the file pointer to be written. String that is to be written. If successful, returns the number of characters written, or FALSE if it fails. Example:

    1. $fp = fopen ("Test.txt", "w");//file is emptied and then written
    2. if ($FP)
    3. {
    4. $count = 0;
    5. for ($i =1; $i <=5; $i + +)
    6. {
    7. $flag =fwrite ($fp, "line". $i. ":". " Hello world!\r\n ");
    8. if (! $flag)
    9. {
    10. echo "Failed to write to file
      ";
    11. Break
    12. }
    13. $count + = $flag;
    14. }
    15. echo "Total write". $count. " Characters ";
    16. }
    17. Else
    18. {
    19. echo "Failed to open file";
    20. }
    21. Fclose ($FP);
    22. ?>
Copy Code

The result of the page output is: Write 100 characters in total

The Test.txt file is written: line 1:hello world! row 2:hello world! row 3:hello world! row 4:hello world! row 5:hello world!

  • Related Article

    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.