Examples of dat file read/write operations in PHP. In php, the fopen function is mostly used for reading files of any type, and other functions are used for subsequent operations. The following describes how to read dat data files. The following is an article in php that uses the fopen function instead of reading any type of files, and then works with other functions to perform operations. next I will introduce how to read dat data files.
The following is an article about basic file read/write operations. after reading this article, I learned about basic file operations and shared it with you: copy the content to the clipboard.
The code is as follows: |
|
$ File_name = "data. dat "; // The absolute path of the file to be read: homedata. dat $ File_pointer = fopen ($ file_name, "r "); // Open the file. "r" is a mode, or the operation method we want to perform. for details, refer to the introduction below. $ File_read = fread ($ file_pointer, filesize ($ file_name )); // Read the file content through the file pointer Fclose ($ file_pointer ); // Close the file Print "the file content read is: $ file_read "; // Display the file content ?> |
Code:
The code is as follows: |
|
$ File_name = "data. dat "; // Absolute path: homedata. dat $ File_pointer = fopen ($ file_name, "w "); // "W" is a mode. for details, refer to the following section. Fwrite ($ file_pointer, "what you wanna write "); // Cut the file to 0 bytes and write Fclose ($ file_pointer ); // End Print "data is successfully written to the file "; ?> |
Code:
The code is as follows: |
|
$ File_name = "data. dat "; // Absolute path: homedata. dat $ File_pointer = fopen ($ file_name, ""); // "W" mode Fwrite ($ file_pointer, "what you wanna append "); // Do not cut the file into 0 bytes and append the data to the end of the file Fclose ($ file_pointer ); // End Print "data is successfully appended to the file "; ?> |
The above is just a brief introduction. next we will discuss some deeper ones.
Sometimes many people write data (most often on websites with large traffic), and useless data is generated to write files. for example:
The content of the info. file 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 | in the previous example of USA, when PHP writes Rob information to the file, Bill also begins writing data. at this time, it is necessary to write 'n' of Rob's record ', cause file damage.
We certainly don't want this to happen, so let's look at the file lock: copy the content to the clipboard
Code:
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 you may need to write LOCK_SH directly as 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, use 3 instead of LOCK_UN } Fclose ($ file_pointer ); |
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 content to clipboard
Code:
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 instead of 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, use 3 instead of LOCK_UN } Fclose ($ file_pointer ); Print "data is successfully written to the file "; ?> |
Hmmm... the append data is a bit different from other operations, that is, FSEEK! It is always a good habit to make sure that the file pointer is at the end of the file.
In Windows, ''must be added before the file name ''.
FLOCK Miscellaneous:
Flock () is locked only after the file is opened. In the above column, the file is locked only after it is opened. the content of the file is only in the current time, but does not reflect the results of other Program Operations. Therefore, it is not just an append operation on the file, fseek should also be used for read operations.
(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.
Bytes. The following is an article...