Knowledge Points:
one, fopen (), File Open function, Read and write Parameters are:
1, R: read-only, Pointer at the beginning of the file
2, r+: Read and write, pointer ibid.
3, W: write-only, Delete the contents of the file before writing, then the pointer back to the beginning of the file, the file does not exist to create
4, W+: Read and write, other IBID.
5, A: Write only, the pointer at the end of the file, the file does not exist is created, note: this mode is "append"
6, A: Read and write, ibid.
7. X: Create and open file (mode: write only), return a e_warning error if the file already exists
8. X+: Create and open file (mode: read/write), other Ibid.
Summary: R Read and write good, W will delete the file content, A pointer will appear at the end of the file, the write operation is appended, so the pointer offset function fseek invalid, x is generally a new file
second, read Files: file () and file_get_contents ()
File () returns an array, file_get_contents () returns a string
three, file Write operation: fwrite ()
The first argument is a file pointer, The second argument is the passed-in string
note: If the file pointer is in the same location as the data, the string overrides, and so on, the subsequent data is overwritten until the incoming string is Finished.
Iv. file pointer offset: fseek ()
The first parameter is the file pointer, The second argument is the offset (int Type) byte, The third parameter determines the initial position of the file pointer, and the second parameter determines the position of the final pointer.
Available options for the third parameter:
①, Seek_cur: Current position of the pointer
②, seek_end: End of file, if you want to offset forward, The offset must be a negative number.
③, seek_set: File Start location
⑤, Get offset: Ftell ()
parameter is a file pointer
Code:
<?phpheader ("content-type:text/html;charset=gbk"); $jubing = fopen ("c:\users\administrator\desktop/test.txt", " $str = mb_convert_encoding ("r+", ' gbk ', ' utf-8 '); fseek ($jubing, 3,seek_cur); fwrite ($jubing, $str); $newstr = File_ Get_contents ("c:\users\administrator\desktop/test.txt"); echo $newstr; var_dump (fclose ($jubing));//file closed successfully?>
File Initial Content:
After the file read and write Content:
PHP file operation function and file pointer understanding