In php, the fopen function is mostly used to read files of any type, and then operated with other functions. The following describes how to read dat data files. the following is an article about basic file read/write operations. I used to learn this article after reading it... in php, the fopen function is mostly used to read files of any type, and then operated with other functions. The following describes how to read dat data files.
The following is an article about basic file read/write operations. I used to read this article and learn about basic file operations. here I will share it with you and copy the content to the clipboard, the code is as follows:
$ File_name = "data. dat "; // absolute path of the file to be read: homedata. dat $ file_pointer = fopen ($ file_name, "r"); // open a file. "r" is a mode, or the operation method we want to perform, for more information, see $ file_read = fread ($ file_pointer, filesize ($ file_name); // read the file content fclose ($ file_pointer) through the file pointer ); // close the file print "the file content read is: $ file_read"; // display the file content $ file_name = "data. dat "; // absolute path: homedata. dat $ file_pointer = fopen ($ file_name, "w"); // "w" is a mode. for details, see fwrite ($ file_pointer, "what you wanna write "); // cut the file to 0 bytes, and then write fclose ($ file_pointer); // end print "data successfully written to the file"; $ file_name = "data. dat "; // absolute path: homedata. dat // open source code phprm.com $ file_pointer = fopen ($ file_name, "a"); // "w" mode fwrite ($ file_pointer, "what you wanna append "); // do not cut the file into 0 bytes, and append the data to the final fclose ($ file_pointer) of the file; // end print "The data is successfully appended to the file ";
The above is just a brief introduction. next we will discuss some deeper things. sometimes many people write data. The most common is that, on websites with large traffic, useless data is generated to write files, for example, info. the file content is as follows:
->
| 1 | Mukul | 15 | Male | India (n)
| 2 | Linus | 31 | Male | Finland (n) two people register at the same time, causing file corruption
->
Info. file->
| 1 | Mukul | 15 | Male | India
| 2 | Linus | 31 | Male | Finland
| 3 | Rob | 27 | Male | USA | Bill | 29 | Male | USA
In the above example, when PHP writes Rob's information to the file, Bill also begins writing. at this time, it is necessary to write 'n' of Rob's record, causing file damage.
We certainly don't want this to happen, so let's look at the file lock and copy the content to the clipboard. the code is as follows:
$ File_name = "data. dat "; $ file_pointer = fopen ($ file_name," r "); $ lock = flock ($ file_pointer, LOCK_SH); // I use 4.0.2, so I use LOCK_SH, you may need to write 1. if ($ lock) {$ file_read = fread ($ file_pointer, filesize ($ file_name); $ lock = flock ($ file_pointer, LOCK_UN); // if the version is earlier than PHP4.0.2, replace LOCK_UN} fclose ($ file_pointer) with 3; print "the file content is $ file_read ";
In the preceding example, if two files are read. php and read2.php both need to access the file, so they can be read, but when a program needs to write, it must wait until the read operation is complete and the file is released, copy the content to the clipboard. the code is as follows:
$ File_name = "data. dat "; $ file_pointer = fopen ($ file_name," w "); $ lock = flock ($ file_pointer, LOCK_EX); // if the version is earlier than PHP4.0.2, use 2 to replace LOCK_EX if ($ lock) {fwrite ($ file_pointer, "what u wanna write"); flock ($ file_pointer, LOCK_UN); // if the version is earlier than PHP4.0.2, replace LOCK_UN} fclose ($ file_pointer) with 3; print "data is successfully written to the file ";
Append data is a little different from other operations, that is, FSEEK. it is always a good habit to confirm that the file pointer is at the end of the file. in Windows, you need to add ''before the file name in the above file ''.
FLOCK Miscellaneous: Flock () is locked only after the file is opened. it is locked only after the file is opened in the upper column. the file content is only the content at the time, does not reflect the results of other program operations, so it is not just an append operation in the file, but also the read operation should use fseek. the translation here may not be very accurate, but I have figured it out.
Mode:
'R'-Open in read-only mode, and place the file pointer in the file header
'R + '-Open in read/write mode, and place the file pointer in the file header
'W'-Write-only open, the file pointer is placed in the file header, the file is cut to 0 bytes, if the file does not exist, try to create a file
'W + '-open read/write. the file pointer is placed in the file header. the file size is cut to 0 Bytes. if the file does not exist, try to create a file.
'A'-Open in write-only mode. the file pointer is placed at the end of the file. if the file does not exist, try to create a file.
'A + '-open read/write. the file pointer is placed at the end of the file. if the file does not exist, try to create the file.
Address:
Reprinted at will, but please attach the article address :-)