The PHPfopen function reads and writes txt files.

Source: Internet
Author: User
Below we use several examples to summarize the use of the phpfopen function to implement file read and write operations. if you need to learn, refer. For a simple reference, refer to the fopen function fopen () function to open a file or URL. If opening fails, this function returns FALSE.

Below we use several examples to summarize the use of the php fopen function to implement file read and write operations. if you need to learn, refer.

For a simple reference, see the fopen function fopen () function to open a file or URL.

If opening fails, this function returns FALSE.

Syntax

Fopen (filename, mode, include_path, context)

The instance code for creating a file is as follows:

  1. If (! File_exists ("test.txt") {// if the file does not exist (the default value is in the current directory)
  2. $ Fh = fopen ("test.txt", "w ");
  3. Fclose ($ fh); // close the file
  4. }
  5. ?>

The code for modifying and editing a robots file is as follows:

  1. Function get_txt ($ robots_file)
  2. // Define the function. the content is included in {}.
  3. {
  4. If (file_exists ($ robots_file ))
  5. // If the object exists, read the content.
  6. {
  7. $ Fp = @ fopen ($ robots_file, "r ");
  8. // R is the abbreviation of read, which indicates reading. open a file in read-only mode.
  9. If ($ fp ){
  10. While (! Feof ($ fp) {// if it is not at the end of the file
  11. $ Robots_txt = fgets ($ fp, 4096); // read data row by row
  12. $ Robots_all = $ robots_all. $ robots_txt; // save the data to $ robots_all.
  13. }
  14. Return ($ robots_all); // return all content
  15. Fclose ($ fp); // close the file
  16. }
  17. }
  18. }
  19. Function put_txt ($ robots_txt)
  20. {
  21. $ Fp = fopen ("robots.txt", "w ");
  22. // W is the abbreviation of write, which indicates the meaning of writing. open the file as a write.
  23. Fputs ($ fp, $ robots_txt );
  24. // Output text to a file
  25. Fclose ($ fp );
  26. }
  27. ?>
  28. $ Edit = $ _ GET ["edit"];
  29. $ Txt = $ _ POST ["txt"];
  30. $ Get_txt = get_txt ("robots.txt ");
  31. // Call the defined function to open the robots file.
  32. If ($ edit = "write ")
  33. {
  34. Put_txt ($ txt );
  35. Echo "success save and return ";
  36. }
  37. Else
  38. {
  39. Echo "returned results of successful reading of robots.txt ";
  40. }
  41. ?>
  42. If ($ edit = "")
  43. {
  44. ?>
  45. }
  46. ?>

Read the data in the counter.txt file through php and save it to the text document.

Create a counter. php document and enter the following code. Unlike ASP, single-line comments in PHP are implemented with // or #, and multi-line comments are implemented:

The instance code is as follows:

  1. Function get_hit ($ counter_file)
  2. // Define the function. the content is included in {}. it should be similar to the C language.
  3. {
  4. $ Count = 0;
  5. // Returns the counter to zero. add the $ sign before the variable in Php.
  6. If (file_exists ($ counter_file ))
  7. // If the counter file exists, read the content
  8. {
  9. $ Fp = fopen ($ counter_file, "r ");
  10. // R is the abbreviation of read, which indicates reading. open a file in read-only mode.
  11. $ Count = 0 + fgets ($ fp, 20 );
  12. /* Assign the value of the first 20 digits to the count variable. because the fgets () function reads strings, you must convert them to integers by adding + 0,
  13. This is different from ASP. Strings in ASP can be directly computed with integers without conversion .*/
  14. Fclose ($ fp );
  15. // Close the file
  16. }
  17. $ Count ++;
  18. // Increase the count, which is very similar to C.
  19. $ Fp = fopen ($ counter_file, "w ");
  20. // W is the abbreviation of write, which indicates the meaning of writing. open the file as a write.
  21. Fputs ($ fp, $ count );
  22. // Output the count value to the file
  23. Fclose ($ fp );
  24. Return ($ count );
  25. // Return the count value
  26. }
  27. ?>
  28. $ Hit = get_hit ("counter.txt ");
  29. // Call the just-defined counter to process the counter.txt document and assign the result to the hit variable.
  30. Echo "you are the first"." $ Hit "."Visitor! ";
  31. // The difference between output result. PHP and ASP is that the connection character of ASP is "&", while that of Php is ".".
  32. ?>

Also insert this file in the PHP document to be called:

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.