Resources (resource) The word is often linked with an entity may initiate an input or output stream. Standard input or output file and network sockets are examples of resources. Therefore, you will often see a lot of functions described in this section are discussed in the context of the resources to deal with, rather than document processing, which in itself is because these functions are able to use all of the resources in conjunction with the front, however, Because these functions are used in conjunction with file applications, the most common.
Write data to a file, there are three steps:
1. Open the file, if the file does not exist, create
2. The data is written to the file.
3. Close the file
Similarly, data is read from a file, there are three steps:
1. Open the file, if the file can not be opened, they should be aware of this and correctly exit.
2. read data from the file
3. Close the file.
open a file
To open a file in PHP, you can use fopn () function, when you open a file, you need to specify how to use it, that is, the file mode
Select the file mode: When you open a file, there are 3 options:
1. Open the file as read-only, write-only or read and write.
2. If you want to write a file, you may want to cover all existing content files, or simply append new data to the end did not file. If the file already exists, it can also terminate execution of the program rather than overwriting the file.
3. If you want to write a distinction between a file system on a binary mode and plain text, but also need to specify the methods used.
Function fopen () in the above embodiment of the support 3 in combination.
$ Fp = fopen (file path, file mode, [whether to search for a file in the include_path] [filename protocol allows
Name start (such as http: //)])
File Mode function fopen
Mode Mode name meaning
r read-only file pointer at the beginning of the file
r + write pointer at the beginning of the file document
w write only before writing, deleting files, position the pointer returned
To the beginning of the file, if the file does not exist, create
w + read prior to reading or writing, file contents are deleted, the pointer
Return to the beginning of the file does not exist, it is created
a write-only file pointer at the end of the file, and if does not exist, it is created. this
Additional mode called (the append)
a + write pointer at the end of the document file. If not, it is created.
This process is known as appended to the file
b binary
t text for the set of other modes, the mode is only the window system
An option, it is not a recommended option, unless you have in your code
used.
If fopen function fails, the function returns false, you can use a user-friendly way to handle this error, and may give an error message according to their own way by suppressing PHP's error message.
$ Fl = fopen ( 'file.txt', 'r +'); // in my E drive to create a file file.txt
Write file
Write files in PHP is relatively simple. You can use fwrite () or fputs () function, we can use the following call to fwrite way
();
fwrite($fp,$outstring,strlen($outstring));
The first argument: open file
The second parameter: the text string to be written
The third parameter: Maximum string
fwrite () function a new function is replaced file_put_cotents ();
<?php $ Fl = fopen ( 'file.txt', 'r +'); // in my E drive to create a file file.txt $ Str = 'Hello, please contact me to see. '; fwrite ($ fl, $ str, strlen ($ str)); // write the text in this file ?>
file_put_contents ( 'file2.txt', 'China Hello'); // This only applies a data write
Read file
$fp=fopen(‘file.txt’,'r');
fgetc (): read the first character string and the pointer to the next
fgets (); read a line of text to be displayed specified line length
fgetss (): read a line from the file pointer flag and filtered off HTMLI
fread (): read quantitative characters.
fpassthru (): output file only care about you at all remaining data
file (): read the entire file array to row grouping
readfile (): read a file and written to the output buffer
file_get_contents (): read the entire file into a string