PHP file operation function and file pointer understanding

Source: Internet
Author: User

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

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.