The following is an article on basic document reading and writing operations, I used to read this article after the basic operation of the document, issued to share with everyone: copy content to the Clipboard
The code is as follows |
Copy Code |
$file _name = "Data.dat"; Absolute path of the file to read: Homedata.dat $file _pointer = fopen ($file _name, "R"); Open the file, "R" is a pattern, or we want to do the operation method, as described in the following article $file _read = fread ($file _pointer, FileSize ($file _name)); Reading file contents through file pointers Fclose ($file _pointer); Close File Print "Read the contents of the file is: $file _read"; Show file contents ?> |
Code:
The code is as follows |
Copy Code |
$file _name = "Data.dat"; Absolute path: Homedata.dat $file _pointer = fopen ($file _name, "w"); "W" is a pattern, as detailed in the following Fwrite ($file _pointer, "What You Wanna Write"); First cut the file to 0 byte size, and then write Fclose ($file _pointer); End Print "Data is successfully written to file"; ?> |
Code:
The code is as follows |
Copy Code |
$file _name = "Data.dat"; Absolute path: Homedata.dat $file _pointer = fopen ($file _name, "a"); "W" mode Fwrite ($file _pointer, "What You Wanna Append"); Do not cut the file into 0 bytes, append the data to the end of the file Fclose ($file _pointer); End Print "Data was successfully appended to file"; ?> |
The above is just a brief introduction, below we want to discuss some deeper.
There are times when multiple writes are occurring (most often in large traffic sites), resulting in useless data being written to files, such as:
The contents of the Info.file file are as follows
->
|1| mukul|15| male| India (N)
|2| linus|31| male| Finland (N) Now two people registered at the same time, causing file damage
->
Info.file->
|1| mukul|15| male| India
|2| linus|31| male| Finland
|3| rob|27| male| usa|
bill|29| male| In the example in the USA, when PHP writes Rob's information to the file, Bill just starts writing, which is exactly what he needs to write to Rob's record ' n ' to cause file damage.
We certainly don't want this to happen, so let's look at file locking: Copy content to clipboard
Code:
The code is as follows |
Copy Code |
$file _name = "Data.dat"; $file _pointer = fopen ($file _name, "R"); $lock = Flock ($file _pointer, lock_sh); I use 4.0.2, so with lock_sh, you may need to write directly to 1. if ($lock) { $file _read = fread ($file _pointer, FileSize ($file _name)); $lock = Flock ($file _pointer, Lock_un); If the version is less than PHP4.0.2, replace Lock_un with 3 } Fclose ($file _pointer); |
Print "File content is $file _read";
?> in the example above, if two files read.php and read2.php have access to the file, they can all be read, but when a program needs to be written, it must wait until the read operation is complete and the file is released. Copy Content to Clipboard
Code:
The code is as follows |
Copy Code |
$file _name = "Data.dat"; $file _pointer = fopen ($file _name, "w"); $lock = Flock ($file _pointer, LOCK_EX); If the version is lower than PHP4.0.2, replace LOCK_EX with 2 if ($lock) { Fwrite ($file _pointer, "What U Wanna Write"); Flock ($file _pointer, Lock_un); If the version is lower than PHP4.0.2, replace Lock_un with 3 } Fclose ($file _pointer); Print "Data is successfully written to file"; ?> |
Hmmm., for appending data is a little different from other operations, which is fseek! It is always a good habit to confirm that the file pointer is at the end of the file.
If you are on a Windows system, the file name above should be preceded by a ".
Flock of the following topics:
Flock () is locked only when the file is open. After the file is opened before the lock, now the contents of the file is only at the current content, and does not reflect the results of other program operations, so not only in the file append operation, or read operation should also use Fseek.
(the translation may not be very exact here, but I want to mean it).
About Patterns:
' R '-read-only open, file pointer placed on file header
' r+ '-read-write mode open, file pointer placed on file header
' W '-write-only open, file pointer placed on file header, file cut to 0 bytes, if file does not exist, try to create file
' w+ '-read and write open, file pointer placed on file header, file size cut to 0 bytes, if file does not exist, try to create file
' A '-write-only mode open, file pointer at end of file, if file does not exist, try to create file
' A + '-read and write open, file pointer at end of file, if file does not exist, try to create file