For a long time did not read the PHP manual, some of the knowledge of the operation of the file has not been learned, now tutorial, by the way:
1. opening of the file:fopen () The first parameter of this function contains the name of the file to be opened, and the second parameter specifies which mode to use to open the file
<! DOCTYPE html>PHP $myfile fopen ("webdictionary.txt", "Rdie" ("Unable to open file!" ); Echo fread ($myfile,filesize("Webdictionary.txt")); fclose ($myfile);? ></body>
Operation Result:
AJAX = asynchronous JavaScript and XML CSS = cascading Style Sheets HTML = Hyper Text Markup Language php = PHP Hypertext Preprocessor SQL = structured Query Language SVG = Scalable Vector Graphics XML = extensible Markup Language
2. file closed:fclose () The name of the file to be closed (or a variable that holds the file name) is commented: It is a good programming habit to close all of the files after they are exhausted.
Example:
<? PHP $myfile fopen ("Webdictionary.txt", "R"); // some code to be executed .... fclose ($myfile);? >
3. file creation :fopen () when the open file does not exist and is opened to write (W) or add (a) when the file is created.
$myfile fopen ("Testfile.txt", "W")
4. file reads :ReadFile ( ) and fread () Difference: the ReadFile () function reads the file and writes it to the output buffer without the need for code to manipulate the open process ; Fread () Function reads an open file, the first parameter contains the file name of the file to be read, and the second parameter specifies the maximum number of bytes to be read
Example:
<? PHP Echo ReadFile ("Webdictionary.txt");? >
fread ($myfile,filesize("Webdictionary.txt"));
Read single-line file:fgets () to read a single line from a file
<? PHP $myfile fopen die ("Unable to open file!" ); Echo fgets ($myfile); fclose ($myfile);? >
Readsingle character: Fgetc () to read individual characters from a file
<? PHP $myfile fopen die ("Unable to open file!" ); // output single character until End-of-file while (! feof ($myfile)) { echofgetc($myfile);} fclose ($myfile);? >
5. check if the bottom of the file has been reached:feof ()
<? PHP $myfile fopen die ("Unable to open file!" ); // output single line until End-of-file while (! feof ($myfile)) { echofgets($myfile). "<br>";} fclose ($myfile);? >
6. write to File:fwrite () The first parameter contains the file name of the file to be written, and the second argument is the string to be written.
The following example writes the name to a new file named "Newfile.txt":
<? PHP $myfile fopen die ("Unable to open file!" ); $txt = "Bill gates\n"; fwrite ($myfile$txt); $txt = "Steve jobs\n"; fwrite ($myfile$txt); fclose ($myfile);? >
If we open the "newfile.txt" file, it should look like this:
Bill Gatessteve Jobs
7. coverage of the article:overwritimg () What happens when you write to an existing file. All existing data is erased and starts with a new file.
<? php $myfile = fopen ("Newfile.txt", "w") or " Span style= "COLOR: #0000ff" >die ("Unable to open file!" $txt = "Mickey mouse\n" ; overwritimg ( $myfile , $txt $txt = "Minnie mouse\n" ; overwritimg ( $myfile , $txt fclose ( $myfile ?
If we open this "newfile.txt" file Now, Bill and Steve have disappeared, leaving only the data we have just written:
Mickey Mouseminnie Mouse
Handling and operation of PHP files