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 ...