PHP file writing, reading and replacing content _ PHP Tutorial

Source: Internet
Author: User
Write, read, and replace php files. Perfect for PHP writing, reading, and replacing file content. Fopen (file name. extension, operation method) fwrite (read file, written file); fclos is perfect for PHP writing, reading, and replacing file content. First, I will explain it in the following ways:

  • Fopen ("file name. extension", "Operation method ")
  • Fwrite (read file, "written file ");
  • Fclose (Open Object variable );
// Write the file $ str = "This is a test from www. bkjia. comn "; // w indicates opening the file as a write. if the file does not exist, the system automatically creates $ file_pointer = fopen (" aa.txt "," a + "); fwrite ($ file_pointer, $ str); fclose ($ file_pointer); // read the file $ file_name = "aa.txt"; $ fp = fopen ($ file_name, 'r '); while (! Feof ($ fp) {$ buffer = fgets ($ fp, 4096); // replace the file $ buffer = str_replace ("bkjia", "Modern Magic", $ buffer ); $ buffer = str_replace ("This is a test", "This is an instance", $ buffer); echo $ buffer."
";} Fclose ($ fp );
Fopen ()

The fopen () function is used to open a file in PHP. The first parameter of this function contains the name of the file to be opened, and the second parameter specifies the mode used to open the file.

Mode description

  • R read-only. Start at the beginning of the file.
  • R + read/write. Start at the beginning of the file.
  • W write only. Open and clear the file content. if the file does not exist, create a new file.
  • W + read/write. Open and clear the file content. if the file does not exist, create a new file.
  • A append. Open and write to the end of the file. if the file does not exist, create a new file.
  • A + read/append. Write content to the end of the file to keep the file content.
  • X write only. Create a new file. If the object already exists, FALSE is returned.
  • X + read/write. Create a new file. If the file already exists, FALSE and an error are returned.

Note: If fopen () cannot open the specified file, 0 (false) is returned ).

Feof ()

Detect End-of-file

The feof () function checks whether the object has reached the end of the object (EOF). The feof () function is useful when traversing data with unknown lengths cyclically. Note: in w, a, and x modes, you cannot read open files!

if (feof($file)) echo "End of file";
Fgets ()

The fgets () function is used to read objects row by row from objects. After this function is called, the file pointer is moved to the next row.

Bytes. Fopen ("file name. extension", "Operation method") fwrite (read file, "written file"); fclos...

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.