-
-
// Note that the !== operator does not exist before 4.0.0-RC2
if ($handle = opendir(' /path/to/files')) {
- echo "Directory handle: $handlen";
- echo "Files:n";
- while (false !== ($file = readdir ($handle))) {
- echo "$filen";
- }
- while ($file = readdir($handle)) {
- echo "$filen";
- }
closedir( $handle);
- }
- ?>
-
Copy code
Read file:
-
- $file_name = "data.dat";
- // Absolute path of the file to be read: homedata.dat
- $file_pointer = fopen($file_name, "r");
- // Open the file, 8. "r" is a mode, 9. Or the operation method we want to perform, 10. See the introduction later in this article for details
- $file_read = fread($file_pointer, filesize($file_name));
- // Read the file content through the file pointer
- fclose($file_pointer);
- // Close the file
- print "The read file content is: $file_read";
- // Display the file content
- ?>
Copy code
Write file:
-
- $file_name = "data.dat";
- // Absolute path: homedata.dat
- $file_pointer = fopen($file_name, "w");
- // "w" is A mode, 8. See details later
- fwrite($file_pointer, "what you wanna write");
- // First cut the file 12. to 0 bytes, 13. then write
- fclose($file_pointer) ;
- // End
- print "Data successfully written to file";
- ?>
Copy the code
and append it to the end of the file:
-
-
- $file_name = "data.dat";
- // Absolute path: homedata.dat
$file_pointer = fopen($file_name , "a");
- // "w" mode
- fwrite($file_pointer, "what you wanna append");
- // No 11. Cut the file 12. into 0 bytes, 13. Append the data to At the end of the file
- fclose($file_pointer);
- // End
- print "Data successfully appended to the file";
- ?>
-
-
Copy code
The above is just a brief introduction, below we will Discuss something deeper.
Sometimes multiple people write (most commonly on websites with large traffic), which results in useless data being written to the file, for example:
info.file file content is as follows ->
|1|Mukul|15|Male|India (n)
|2|Linus|31|Male|Finland (n)
Now 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 happens to start writing. At this time, it happens that the 'n' of Rob's record needs to be written, causing the file to be damaged.
We certainly don’t want this to happen, so let’s look at file locking:
-
- $file_name = "data.dat";
- $file_pointer = fopen($file_name, "r");
- $lock = flock($file_pointer, LOCK_SH);
- // Me Use 4.0.2, 9. So use LOCK_SH, 10. You may need to write it directly as 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, 17. Replace LOCK_UN with 3
- }
- fclose($file_pointer);
- print "The file content is $file_read";
- ?>
-
Copy code
In the above example, if the two files read.php and read2.php both need to access the file, then they can both read it, but when a program needs to write, it must wait until the read operation is completed. , the file is released.
-
- $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, 9. Use 2 instead of LOCK_EX
- if ($lock) {
- fwrite($file_pointer, "what u wanna write");
- flock($file_pointer, LOCK_UN);
- // If the version is lower than PHP4.0.2, 16. Use 3 instead of LOCK_UN
- }
- fclose($file_pointer);
- print "Data was successfully written to the file";
- ?>
-
Copy code
Although the "w" mode is used to overwrite files, I don't think it is applicable.
-
-
- $file_name = "data.dat";
- $file_pointer = fopen($file_name, "a");
- $lock = flock($file_pointer, LOCK_EX) ;
- // If the version is lower than PHP4.0.2, 9. Use 2 instead of LOCK_EX
if ($lock) {
- fseek($file_pointer, 0, SEEK_END);
- // If the version is lower than PHP4.0RC1, 15. Use fseek($file_pointer, filsize($file_name));
fwrite($file_pointer, "what u wanna write");
- flock($file_pointer, LOCK_UN) ;
- // If the version is lower than PHP4.0.2, 20. Use 3 instead of LOCK_UN
- }
- fclose($file_pointer);
- print "Data was successfully written to the file";
- ?>
-
Copy the code
Hmmm..., Appending data is a little different from other operations, it is FSEEK! It is always a good habit to make sure the file pointer is at the end of the file.
If it is under Windows system, the above file needs to be preceded by ''.
FLOCK Miscellaneous Talk:
Flock() only locks the file after it is opened. In the above column, the file is locked after it is opened. Now the content of the file is only the content at that time, and does not reflect the results of other program operations. Therefore, fseek should be used not only for file append operations, but also for read operations.
(The translation here may not be exact, but I think I get the idea).
About the pattern:
'r' - Open read-only, with the file pointer at the beginning of the file
'r+' - Open for reading and writing, place the file pointer at the beginning of the file
'w' - open for writing only, the file pointer is placed at the beginning of the file, the file is cut to 0 bytes, if the file does not exist, try to create the file
'w+' - Open for reading and writing, the file pointer is placed at the beginning of the file, the file size is cut to 0 bytes, if the file does not exist, try to create the file
'a' - Open for writing only, the file pointer is placed at the end of the file, if the file does not exist, try to create the file
'a+' - open for reading and writing, the file pointer is placed at the end of the file, if the file does not exist, try to create the file
By the way, the code to create the file directory
-
-
//Create a directory similar to "../../../xxx/xxx.txt"
- function createdirs($path, $mode = 0777) //mode 077
- {
- $dirs = explode('/',$path);
- $pos = strrpos($path, ".");
- if ($pos === false) { // note: three equal signs
- // not found, means path ends in a dir not file
- $subamount=0;
- }
- else {
- $subamount=1;
- }
for ($c=0;$c < ; count($dirs) - $subamount; $c++) {
- $thispath="";
- for ($cc=0; $cc <= $c; $cc++) {
- $thispath.=$dirs[$ cc].'/';
- }
- if (!file_exists($thispath)) {
- //print "$thispath
- ";
- mkdir($thispath,$mode); //mkdir function creates directory
- }
- }
- }
- //Call such as createdirs("xxx/xxxx/xxxx",);
//The original function used $GLOBALS["dirseparator"] and I changed it to '/'< ;/p>
function recur_mkdirs($path, $mode = 0777) //mode 0777
- {
- //$GLOBALS["dirseparator"]
- $dirs = explode($GLOBALS["dirseparator"], $path);
- $pos = strrpos($path, ".");
- if ($pos === false) { // note: three equal signs
- // not found, means path ends in a dir not file
- $subamount=0;
- }
- else {
- $subamount=1;
- }
-
-
Copy code
The above article is for beginners’ reference, and experts can pass it by.
|