Php file read/write operations

Source: Internet
Author: User
Php file read/write operations

  1. // Note that it does not exist before 4.0.0-RC2! = Operator

  2. If ($ handle = opendir ('/path/to/Files ')){

  3. Echo "Directory handle: $ handle \ n ";
  4. Echo "Files: \ n ";

  5. While (false! ==( $ File = readdir ($ handle ))){
  6. Echo "$ file \ n ";
  7. }
  8. While ($ file = readdir ($ handle )){
  9. Echo "$ file \ n ";
  10. }

  11. Closedir ($ handle );

  12. }
  13. ?>

Read files:

  1. $ File_name = "data. dat ";
  2. // The absolute path of the file to be read: homedata. dat
  3. $ File_pointer = fopen ($ file_name, "r ");
  4. // Open the file. 8. "r" is a mode, 9. or the operation method we want to perform. 10. for details, refer to the introduction below.
  5. $ File_read = fread ($ file_pointer, filesize ($ file_name ));
  6. // Read the file content by referring to 14.
  7. Fclose ($ file_pointer );
  8. // Close the file
  9. Print "the file content read is: $ file_read ";
  10. // Display the file content
  11. ?>

Write file:

  1. $ File_name = "data. dat ";
  2. // Absolute path: homedata. dat
  3. $ File_pointer = fopen ($ file_name, "w ");
  4. // "W" is a mode. 8. for details, refer to the following section.
  5. Fwrite ($ file_pointer, "what you wanna write ");
  6. // Cut the File 12 first. it is 0 bytes, 13. then write
  7. Fclose ($ file_pointer );
  8. // End
  9. Print "data is successfully written to the file ";
  10. ?>

Append to the end of the file:

  1. $ File_name = "data. dat ";

  2. // Absolute path: homedata. dat

  3. $ File_pointer = fopen ($ file_name, "");

  4. // "W" mode
  5. Fwrite ($ file_pointer, "what you wanna append ");
  6. // Not 11. cut the File 12. into 0 bytes, 13. append the data to the end of the file
  7. Fclose ($ file_pointer );
  8. // End
  9. Print "data is successfully appended to the file ";
  10. ?>

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)

Now two people register at the same time, causing file destruction->

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:

  1. $ File_name = "data. dat ";
  2. $ File_pointer = fopen ($ file_name, "r ");
  3. $ Lock = flock ($ file_pointer, LOCK_SH );
  4. // I use 4.0.2, 9. so I use LOCK_SH, 10. you may need to write 1 directly.
  5. If ($ lock ){
  6. $ File_read = fread ($ file_pointer, filesize ($ file_name ));
  7. $ Lock = flock ($ file_pointer, LOCK_UN );
  8. // If the version is earlier than PHP4.0.2, 17. use 3 to replace LOCK_UN
  9. }
  10. Fclose ($ file_pointer );
  11. Print "the file content is $ file_read ";
  12. ?>

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.

  1. $ File_name = "data. dat ";
  2. $ File_pointer = fopen ($ file_name, "w ");
  3. $ Lock = flock ($ file_pointer, LOCK_EX );
  4. // If the version is earlier than PHP4.0.2, 9. use 2 instead of LOCK_EX
  5. If ($ lock ){
  6. Fwrite ($ file_pointer, "what u wanna write ");
  7. Flock ($ file_pointer, LOCK_UN );
  8. // If the version is earlier than PHP4.0.2, 16. use 3 to replace LOCK_UN
  9. }
  10. Fclose ($ file_pointer );
  11. Print "data is successfully written to the file ";
  12. ?>

Although the "w" mode is used to overwrite files, I don't think it is applicable.

  1. $ File_name = "data. dat ";

  2. $ File_pointer = fopen ($ file_name, "");
  3. $ Lock = flock ($ file_pointer, LOCK_EX );
  4. // If the version is earlier than PHP4.0.2, 9. use 2 instead of LOCK_EX

  5. If ($ lock ){

  6. Fseek ($ file_pointer, 0, SEEK_END );
  7. // If the version is earlier than PHP4.0RC1, 15. use fseek ($ file_pointer, filsize ($ file_name ));

  8. Fwrite ($ file_pointer, "what u wanna write ");

  9. Flock ($ file_pointer, LOCK_UN );
  10. // If the version is earlier than PHP4.0.2, 20. use 3 to replace LOCK_UN
  11. }
  12. Fclose ($ file_pointer );
  13. Print "data is successfully written to the file ";
  14. ?>

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: open in 'r'-read-only mode, put the file pointer in the file header 'R + '-Open in read/write mode, and put the file pointer in the file header 'W'-open only, the file pointer is placed in the file header, and the file is cut to 0 Bytes. if the file does not exist, try to create a file 'W + '-read/write to open. the file pointer is placed in the file header, and 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, and put the file pointer 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.

By the way, the code for creating a file directory

  1. // Create a directory similar to "../xxx/xxx.txt"

  2. Function createdirs ($ path, $ mode = 0777) // mode 077
  3. {
  4. $ Dirs = explode ('/', $ path );
  5. $ Pos = strrpos ($ path ,".");
  6. If ($ pos = false) {// note: three equal signs
  7. // Not found, means path ends in a dir not file
  8. $ Subamount = 0;
  9. }
  10. Else {
  11. $ Subamount = 1;
  12. }

  13. For ($ c = 0; $ c <count ($ dirs)-$ subamount; $ c ++ ){

  14. $ Thispath = "";
  15. For ($ cc = 0; $ cc <= $ c; $ cc ++ ){
  16. $ Thispath. = $ dirs [$ cc]. '/';
  17. }
  18. If (! File_exists ($ thispath )){
  19. // Print "$ thispath
  20. ";
  21. Mkdir ($ thispath, $ mode); // create a directory using the mkdir function
  22. }
  23. }
  24. }
  25. // Call such as createdirs ("xxx/xxxx ",);

  26. // In the original function, I changed $ GLOBALS ["dirseparator"] '/'

  27. Function recur_mkdirs ($ path, $ mode = 0777) // mode 0777

  28. {
  29. // $ GLOBALS ["dirseparator"]
  30. $ Dirs = explode ($ GLOBALS ["dirseparator"], $ path );
  31. $ Pos = strrpos ($ path ,".");
  32. If ($ pos = false) {// note: three equal signs
  33. // Not found, means path ends in a dir not file
  34. $ Subamount = 0;
  35. }
  36. Else {
  37. $ Subamount = 1;
  38. }

The above articles are for beginners.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.