PHP file Write, read and replace content _php tutorial

Source: Internet
Author: User
Tags php write
The perfect implementation of PHP write, read, replace file content. First of all, the main use:

    • fopen ("filename. Extension", "How to")
    • Fwrite (Read the file, "write the File");
    • Fclose (open object variable);
Write file $str= "This is a test from www.bkjia.comn"; W means to open the file as written, and if the file does not exist, the system will automatically establish $file_pointer = fopen ("Aa.txt", "A +");        Fwrite ($file _pointer, $str); fclose ($file _pointer);//Read file $file _name= "Aa.txt"; $FP =fopen ($file _name, ' R '); while (!feof ($fp)) {  $buffer =fgets ($fp, 4096);  Replace file  $buffer = Str_replace ("Bkjia", "Modern Magic", $buffer);  $buffer = Str_replace ("This is a test", "It is an instance", $buffer);  echo $buffer. "
"; } Fclose ($FP);

fopen ()

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

Pattern description

    • R is read-only. Start at the beginning of the file.
    • r+ read/write. Start at the beginning of the file.
    • W write only. Open and empty the contents of the file, or create a new file if the file does not exist.
    • w+ read/write. Open and empty the contents of the file, or create a new file if the file does not exist.
    • A append. Opens and writes to the end of the file file, creating a new file if the file does not exist.
    • A + read/append. Keep the contents of the file by writing to the end of the file.
    • X write only. Creates a new file. Returns FALSE if the file is present.
    • x+ read/write. Creates a new file. If the file already exists, it returns FALSE and an error.

Note: if fopen () cannot open the specified file, 0 (FALSE) is returned.

Feof ()

Detection End-of-file

The feof () function detects if the end of the file (EOF) has been reached, and the feof () function is useful when iterating through data of unknown length. Note: In W, a, and x mode, you cannot read open files!

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

Fgets ()

The fgets () function is used to read a file from file to line. After the function is called, the file pointer moves to the next line.

http://www.bkjia.com/PHPjc/752504.html www.bkjia.com true http://www.bkjia.com/PHPjc/752504.html techarticle The perfect implementation of PHP write, read, replace file content. First of all, the main use: fopen ("filename. Extension", "operation Mode") fwrite (read file, "write 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.